Part Number Hot Search : 
703V3 P78083 9P0FZ0 00AXI MN3395 FM206 XFVOIP FSQ500
Product Description
Full Text Search
 

To Download ATMEGA161-4JC Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  1 features ? high-performance, low-power avr ? 8-bit microcontroller  advanced risc architecture ? 130 powerful instructions - most single clock cycle execution ? 32 x 8 general purpose working registers ? fully static operation ? up to 8 mips throughput at 8 mhz ? on-chip 2-cycle multiplier  program and data memories ? 16k bytes of nonvolatile in-system programmable flash endurance: 1,000 write/erase cycles ? optional boot code memory with independent lock bits self-programming of program and data memories ? 512 bytes nonvolatile in-system programmable eeprom endurance: 100,000 write/erase cycles ? 1k bytes internal sram ? programming lock for software security  peripheral features ? two 8-bit timer/counters with separate prescaler and pwm ? expanded 16-bit timer/counter system with separate prescaler, compare, capture modes and dual 8-, 9- or 10-bit pwm ? dual programmable serial uarts ? master/slave spi serial interface ? real time counter with separate oscillator ? programmable watchdog timer with separate on-chip oscillator ? on-chip analog comparator  special microcontroller features ? power-on reset and programmable brown-out detection ? external and internal interrupt sources ? three sleep modes: idle, power save and power-down  i/o and packages ? 35 programmable i/o lines ? 40-pin pdip, 44-pin plcc and tqfp  operating voltages ? 2.7v - 5.5v (atmega161l), 4.0v - 5.5v (atmega161)  speed grades ? 0 - 4 mhz (atmega161l), 0 - 8 mhz (atmega161)  commercial and industrial temperature ranges 8-bit microcontroller with 16k bytes in-system programmable flash atmega161 atmega161l advance information rev. 1228a ? 08/99
atmega161(l) 2 pin configurations description the atmega161 is a low-power cmos 8-bit microcontroller based on the avr risc architecture. by executing powerful instructions in a single clock cycle, the atmega161 achieves throughputs approaching 1 mips per mhz allowing the sys- tem designer to optimize power consumption versus processing speed.the avr core combines a rich instruction set with 32 general purpose working registers. all the 32 registers are directly connected to the arithmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is more code efficient while achieving throughputs up to ten times faster than conventional cisc microcontrollers. the atmega161 provides the following features: 16k bytes of in-system- or self-programmable flash, 512 bytes eeprom, 1k bytes sram, 35 general purpose i/o lines, 32 general purpose working registers, real time counter, three flexible timer/counters with compare modes, internal and external interrupts, two programmable serial uarts, programma- ble watchdog timer with internal oscillator, an spi serial port and three software selectable power saving modes. the idle mode stops the cpu while allowing the sram, timer/counters, spi port and interrupt system to continue functioning. the power-down mode saves the register and sram contents but freezes the oscillator, disabling all other chip functions until the next external interrupt or hardware reset. in power save mode, the timer oscillator continues to run, allowing the user to maintain a timer base while the rest of the device is sleeping. the device is manufactured using atmel ? s high density nonvolatile memory technology. the on-chip flash program mem- ory can be reprogrammed using the self-programming capability through the bootblock, using an isp through the spi-port, or by using a conventional nonvolatile memory programmer. by combining an enhanced risc 8-bit cpu with in-system programmable flash on a monolithic chip, the atmel atmega161 is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. the atmega161 avr is supported with a full suite of program and system development tools including: c compilers, macro assemblers, program debugger/simulators, in-circuit emulators, and evaluation kits. tqfp 1 2 3 4 5 6 7 8 9 10 11 33 32 31 30 29 28 27 26 25 24 23 (mosi) pb5 (miso) pb6 (sck) pb7 reset (rxd0) pd0 nc* (txd0) pd1 (int0) pd2 (int1) pd3 (tosc1) pd4 (ocia/tosc2) pd5 pa4 (ad4) pa5 (ad5) pa6 (ad6) pa7 (ad7) pe0 (icp/int2) nc* pe1 (ale) pe2 (oc1b) pc7 (a15) pc6 (a14) pc5 (a13) 44 43 42 41 40 39 38 37 36 35 34 12 13 14 15 16 17 18 19 20 21 22 (wr) pd6 (rd) pd7 xtal2 xtal1 gnd nc* (a8) pc0 (a9) pc1 (a10) pc2 (a11) pc3 (a12) pc4 pb4 (ss) pb3 (txd1/ain1) pb2 (rxd1/ain0) pb1 (oc2/t1) pb0 (oc0/t0) nc* vcc pa0 (ad0) pa1 (ad1) pa2 (ad2) pa3 (ad3) * nc = do not connect (can be used in future devices) plcc 7 8 9 10 11 12 13 14 15 16 17 39 38 37 36 35 34 33 32 31 30 29 (mosi) pb5 (miso) pb6 (sck) pb7 reset (rxd0) pd0 nc* (txd0) pd1 (int0) pd2 (int1) pd3 (tosc1) pd4 (oc1a/tosc2) pd5 pa4 (ad4) pa5 (ad5) pa6 (ad6) pa7 (ad7) pe0 (icp/int2) nc* pe1 (ale) pe2 (oc1b) pc7 (a15) pc6 (a14) pc5 (a13) 6 5 4 3 2 1 44 43 42 41 40 18 19 20 21 22 23 24 25 26 27 28 (wr) pd6 (rd) pd7 xtal2 xtal1 gnd nc* (a8) pc0 (a9) pc1 (a10) pc2 (a11) pc3 (a12) pc4 pb4 (ss) pb3 (txd1/ain1) pb2 (rxd1/ain0) pb1 (oc2/t1) pb0 (oc0/t0) nc* vcc pa0 (ad0) pa1 (ad1) pa2 (ad2) pa3 (ad3) * nc = do not connect (can be used in future devices) pdip 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 (oc0/t0) pb0 (oc2/t1) pb1 (rxd1/ain0) pb2 (txd1/ain1) pb3 (ss) pb4 (mosi) pb5 (miso) pb6 (sck) pb7 reset (rxd0) pd0 (txd0) pd1 (int0) pd2 (int1) pd3 (tosc1) pd4 (oc1a/tosc2) pd5 (wr) pd6 (rd) pd7 xtal2 xtal1 gnd vcc pa0 (ad0) pa1 (ad1) pa2 (ad2) pa3 (ad3) pa4 (ad4) pa5 (ad5) pa6 (ad6) pa7 (ad7) pe0 (icp/int2) pe1 (ale) pe2 (oc1b) pc7 (a15) pc6 (a14) pc5 (a13) pc4 (a12) pc3 (a11) pc2 (a10) pc1 (a9) pc0 (a8)
atmega161(l) 3 block diagram figure 1. the atmega161 block diagram programming logic spi uarts pb0 - pb7 vcc gnd + - analog comp ara tor 8-bit data bus data dir. reg. porta data register porta porta drivers pa0-pa7 data dir. reg. portc data register portc portc drivers pc0-pc7 portd drivers pd0 - pd7 data dir. reg. portd data register portb portb drivers porte drivers pe0 - pe2 data reg. porte data dir reg. porte program counter internal oscillator watchdog timer stack pointer program flash mcu control register sram general purpose registers instruction register timer/ counters instruction decoder timing and control oscillator interrupt unit eeprom status register z y x alu reset xtal2 xtal1 control lines data dir. reg. portb data register portd
atmega161(l) 4 pin descriptions vcc supply voltage gnd ground port a (pa7..pa0) port a is an 8-bit bidirectional i/o port. port pins can provide internal pull-up resistors (selected for each bit). the port a output buffers can sink 20 ma and can drive led displays directly. when pins pa0 to pa7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. the port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. port a serves as multiplexed address/data port when using external memory interface. port b (pb7..pb0) port b is an 8-bit bidirectional i/o port with internal pull-up resistors. the port b output buffers can sink 20 ma. as inputs, port b pins that are externally pulled low will source current if the pull-up resistors are activated. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. port b also serves the functions of various special features of the atmega161 as listed on page 80. port c (pc7..pc0) port c is an 8-bit bidirectional i/o port with internal pull-up resistors. the port c output buffers can sink 20 ma. as inputs, port c pins that are externally pulled low will source current if the pull-up resistors are activated. the port c pins are tri-stated when a reset condition becomes active, even if the clock is not running. port c also serves as address high output when using external memory interface. port d (pd7..pd0) port d is an 8-bit bidirectional i/o port with internal pull-up resistors. the port d output buffers can sink 20 ma. as inputs, port d pins that are externally pulled low will source current if the pull-up resistors are activated. the port d pins are tri-stated when a reset condition becomes active, even if the clock is not running. port d also serves the functions of various special features of the atmega161 as listed on page 87. port e (pe2..pe0) port e is a 3-bit bidirectional i/o port with internal pull-up resistors. the port e output buffers can sink 20 ma. as inputs, port e pins that are externally pulled low will source current if the pull-up resistors are activated. the port e pins are tri-stated when a reset condition becomes active, even if the clock is not running. port e also serves the functions of various special features of the atmega161 as listed on page 93. reset reset input. a low level on this pin for more than 500 ns will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. xtal1 input to the inverting oscillator amplifier and input to the internal clock operating circuit. xtal2 output from the inverting oscillator amplifier
atmega161(l) 5 crystal oscillator xtal1 and xtal2 are input and output, respectively, of an inverting amplifier which can be configured for use as an on-chip oscillator, as shown in figure 2. either a quartz crystal or a ceramic resonator may be used. to drive the device from an external clock source, xtal2 should be left unconnected while xtal1 is driven as shown in figure 3. figure 2. oscillator connections note: when using the mcu oscillator as a clock for an external device, an hc buffer should be connected as indicated in the figu re. figure 3. external clock drive configuration xtal2 xtal1 gnd c2 c1 max 1 hc buffer hc
atmega161(l) 6 architectural overview the fast-access register file concept contains 32 x 8-bit general purpose working registers with a single clock cycle access time. this means that during one single clock cycle, one arithmetic logic unit (alu) operation is executed. two operands are output from the register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. six of the 32 registers can be used as three 16-bits indirect address register pointers for data space addressing ? enabling efficient address calculations. one of the three address pointers is also used as the address pointer for the constant table look up function. these added function registers are the 16-bits x-register, y-register and z-register. figure 4. the atmega161 avr risc architecture 8k x 16 program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registers alu status and control interrupt unit spi unit 8-bit timer/counter with pwm and rtc watchdog timer analog comparator 32 i/o lines 512 x 8 eeprom data bus 8-bit avr atmega161 architecture serial uart0 16-bit timer/counter with pwm 8-bit timer/counter with pwm 1024 x 8 data sram direct addressing indirect addressing serial uart1
atmega161(l) 7 the alu supports arithmetic and logic functions between registers or between a constant and a register. single register operations are also executed in the alu. figure 4 shows the atmega161 avr risc microcontroller architecture. in addition to the register operation, the conventional memory addressing modes can be used on the register file as well. this is enabled by the fact that the register file is assigned the 32 lowermost data space addresses ($00 - $1f), allowing them to be accessed as though they were ordinary memory locations. the i/o memory space contains 64 addresses for cpu peripheral functions as control registers, timer/counters, and other i/o functions. the i/o memory can be accessed directly, or as the data space locations following those of the regis- ter file, $20 - $5f. the avr uses a harvard architecture concept ? with separate memories and buses for program and data. the program memory is executed with a two stage pipeline. while one instruction is being executed, the next instruction is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is self-programmable flash memory. with the jump and call instructions, the whole 8k word address space is directly accessed. most avr instructions have a single 16-bit word format. every program memory address contains a 16- or 32-bit instruction. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effec- tively allocated in the general data sram, and consequently the stack size is only limited by the total sram size and the usage of the sram. all user programs must initialize the sp (stack pointer) in the reset routine (before subroutines or interrupts are executed). the 16-bit stack pointer is read/write accessible in the i/o space. the 1k bytes data sram can be easily accessed through the five different addressing modes supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps.
atmega161(l) 8 figure 5. memory maps a flexible interrupt module has its control registers in the i/o space with an additional global interrupt enable bit in the st atus register. all the different interrupts have a separate interrupt vector in the interrupt vector table at the beginning of the program memory. the different interrupts have priority in accordance with their interrupt vector position. the lower the interrupt vector address, the higher the priority. 32 gen. purpose working registers 64 i/o registers internal sram (1024 x 8) $0000 $001f $005f $0060 $045f $0020 $000 $1fff data memory program memory program flash (8k x 16) $0460 external sram (0-63k x 8) $ffff
atmega161(l) 9 general purpose register file figure 6 shows the structure of the 32 general purpose working registers in the cpu. figure 6. avr cpu general purpose working registers all the register operating instructions in the instruction set have direct and single cycle access to all registers. the only exceptions are the five constant arithmetic and logic instructions sbci, subi, cpi, andi, and ori between a constant and a register, and the ldi instruction for load immediate constant data. these instructions apply to the second half of the reg- isters in the register file ? r16..r31. the general sbc, sub, cp, and, and or, and all other operations between two registers or on a single register apply to the entire register file. as shown in figure 6, each register is also assigned a data memory address, mapping them directly into the first 32 loca- tions of the user data space. although not being physically implemented as sram locations, this memory organization provides great flexibility in access of the registers, as the x, y and z-registers can be set to index any register in the file . x-register, y-register, and z-register the registers r26..r31 have some added functions to their general purpose usage. these registers are address pointers for indirect addressing of the data space. the three indirect address registers x, y and z are defined as: figure 7. x, y and z-registers in the different addressing modes, these address registers have functions as fixed displacement, automatic increment and decrement (see the descriptions for the different instructions). 70addr. r0 $00 r1 $01 r2 $02 ? r13 $0d general r14 $0e purpose r15 $0f working r16 $10 registers r17 $11 ? r26 $1a x-register low byte r27 $1b x-register high byte r28 $1c y-register low byte r29 $1d y-register high byte r30 $1e z-register low byte r31 $1f z-register high byte 15 0 x-register 7 0 7 0 r27 ($1b) r26 ($1a) 15 0 y-register 7 0 7 0 r29 ($1d) r28 ($1c) 15 0 z-register 7 0 7 0 r31 ($1f) r30 ($1e)
atmega161(l) 10 alu ? arithmetic logic unit the high-performance avr alu operates in direct connection with all the 32 general purpose working registers. within a single clock cycle, alu operations between registers in the register file are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-functions. atmega161 does also provide a powerful multiplier support- ing both signed/unsigned multiplication and fractional format. see instruction set section for a detailed description. self-programmable flash program memory the atmega161 contains 16k bytes on-chip self- and in-system programmable flash memory for program storage. since all instructions are 16- or 32-bit words, the flash is organized as 8k x 16. the flash memory has an endurance of at least 1000 write/erase cycles. the atmega161 program counter (pc) is 13 bits wide, thus addressing the 8192 program memory locations. see page 95 for a detailed description on flash data downloading. see page 11 for the different program memory addressing modes. eeprom data memory the atmega161 contains 512 bytes of data eeprom memory. it is organized as a separate data space, in which single bytes can be read and written. the eeprom has an endurance of at least 100,000 write/erase cycles per location. the interface between the eeprom and the cpu is described on page 54 specifying the eeprom address registers, the eeprom data register, and the eeprom control register. for the spi data downloading, see page 109 for a detailed description. figure 8. sram organization register file data address space r0 $0000 r1 $0001 r2 $0002 o o r29 $001d r30 $001e r31 $001f i/o registers $00 $0020 $01 $0021 $02 $0022 ?? $3d $005d $3e $005e $3f $005f internal sram $0060 $0061 o $045e $045f
atmega161(l) 11 sram data memory figure 8 shows how the atmega161 sram memory is organized. the lower 1120 data memory locations address the register file, the i/o memory and the internal data sram. the first 96 locations address the register file and i/o memory, and the next 1k locations address the internal data sram. an optional external data memory device can be placed in the same sram memory space. this memory device will occupy the locations following the internal sram and up to as much as 64k - 1, depending on external memory size. when the addresses accessing the data memory space exceeds the internal data sram locations, the memory device is accessed using the same instructions as for the internal data sram access. when the internal data space is accessed, the read and write strobe pins (rd and wr ) are inactive during the whole access cycle. external memory operation is enabled by setting the sre bit in the mcucr register. see ? interface to external memory ? on page 72 for details. accessing external memory takes one additional clock cycle per byte compared to access of the internal sram. this means that the commands ld, st, lds, sts, push and pop take one additional clock cycle. if the stack is placed in external memory, interrupts, subroutine calls and returns take two clock cycles extra because the two-byte program counter is pushed and popped. when external memory interface is used with wait state, two additional clock cycles is used per byte. this has the following effect: data transfer instructions take two extra clock cycles, whereas interrupt, subroutine calls and returns will need four clock cycles more than specified in the instruction set manual. the five different addressing modes for the data memory cover: direct, indirect with displacement, indirect, indirect with pre-decrement, and indirect with post-increment. in the register file, registers r26 to r31 feature the indirect addressing pointer registers. the direct addressing reaches the entire data space. the indirect with displacement mode features a 63 address locations reach from the base address given by the y or z-register. when using register indirect addressing modes with automatic pre-decrement and post-increment, the address registers x, y and z are decremented and incremented. the 32 general purpose working registers, 64 i/o registers and the 1k bytes of internal data sram in the atmega161 are all accessible through all these addressing modes. see the next section for a detailed description of the different addressing modes. program and data addressing modes the atmega161 avr risc microcontroller supports powerful and efficient addressing modes for access to the program memory (flash) and data memory (sram, register file, and i/o memory). this section describes the different addressing modes supported by the avr architecture. in the figures, op means the operation code part of the instruction word. to simplify, not all figures show the exact location of the addressing bits.
atmega161(l) 12 register direct, single register rd figure 9. direct single register addressing the operand is contained in register d (rd). register direct, two registers rd and rr figure 10. direct register addressing, two registers operands are contained in register r (rr) and d (rd). the result is stored in register d (rd).
atmega161(l) 13 i/o direct figure 11. i/o direct addressing operand address is contained in 6 bits of the instruction word. n is the destination or source register address. data direct figure 12. direct data addressing a 16-bit data address is contained in the 16 lsbs of a two-word instruction. rd/rr specify the destination or source register. op rr/rd 16 31 15 0 16 lsbs $0000 $ffff 20 19 data space
atmega161(l) 14 data indirect with displacement figure 13. data indirect with displacement operand address is the result of the y or z-register contents added to the address contained in 6 bits of the instruction word. data indirect figure 14. data indirect addressing operand address is the contents of the x, y, or the z-register. data space $0000 $ffff y or z - register op a n 0 0 5 6 10 15 15 data space $0000 $ffff x, y, or z - register 0 15
atmega161(l) 15 data indirect with pre-decrement figure 15. data indirect addressing with pre-decrement the x, y, or the z-register is decremented before the operation. operand address is the decremented contents of the x, y, or the z-register. data indirect with post-increment figure 16. data indirect addressing with post-increment the x, y, or the z-register is incremented after the operation. operand address is the content of the x, y, or the z-register prior to incrementing. data space $0000 $ffff x, y, or z - register 0 15 -1 data space $0000 $ffff x, y, or z - register 0 15 1
atmega161(l) 16 constant addressing using the lpm instruction figure 17. code memory constant addressing constant byte address is specified by the z-register contents. the 15 msbs select word address (0 - 8k), the lsb selects low byte if cleared (lsb = 0) or high byte if set (lsb = 1). indirect program addressing, ijmp and icall figure 18. indirect program memory addressing program execution continues at address contained by the z-register (i.e. the pc is loaded with the contents of the z-register). $1fff $000 program memory $1fff $000 program memory
atmega161(l) 17 relative program addressing, rjmp and rcall figure 19. relative program memory addressing program execution continues at address pc + k + 1. the relative address k is -2048 to 2047. direct program addressing, jmp and call figure 20. direct program addressing program execution continues at the address immediate in the instruction words. $1fff $000 program memory op 16 21 20 31 15 0 16 lsbs program memory $0000 $1fff
atmega161(l) 18 memory access times and instruction execution timing this section describes the general access timing concepts for instruction execution and internal memory access. the avr cpu is driven by the system clock ? , directly generated from the external clock crystal for the chip. no internal clock division is used. figure 21 shows the parallel instruction fetches and instruction executions enabled by the harvard architecture and the fast-access register file concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the corresponding unique results for functions per cost, functions per clocks, and functions per power-unit. figure 21. the parallel instruction fetches and instruction executions figure 22 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destination register. figure 22. single cycle alu operation the internal data sram access is performed in two system clock cycles as described in figure 23. figure 23. on-chip data sram access cycles system clock ? 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 system clock ? total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 system clock ? wr rd data data address address t1 t2 t3 t4 prev. address read write
atmega161(l) 19 i/o memory the i/o space definition of the atmega161 is shown in the following table: table 1. atmega161 i/o space i/o address (sram address) name function $3f($5f) sreg status register $3e ($5e) sph stack pointer high $3d ($5d) spl stack pointer low $3b ($5b) gimsk general interrupt mask register $3a ($5a) gifr general interrupt flag register $39 ($59) timsk timer/counter interrupt mask register $38 ($58) tifr timer/counter interrupt flag register $37 ($57) spmcr store program memory control register $36 ($56) emcucr extended mcu general control register $35 ($55) mcucr mcu general control register $34 ($54) mcusr mcu general status register $33 ($53) tccr0 timer/counter0 control register $32 ($52) tcnt0 timer/counter0 (8-bit) $31 ($51) ocr0 timer/counter0 output compare register $30 ($50) sfior special function io register $2f ($4f) tccr1a timer/counter1 control register a $2e ($4e) tccr1b timer/counter1 control register b $2d ($4d) tcnt1h timer/counter1 high byte $2c ($4c) tcnt1l timer/counter1 low byte $2b ($4b) ocr1ah timer/counter1 output compare registera high byte $2a ($4a) ocr1al timer/counter1 output compare registera low byte $29 ($49) ocr1bh timer/counter1 output compare registerb high byte $28 ($48) ocr1bl timer/counter1 output compare registerb low byte $27 ($47) tccr2 timer/counter2 control register $26 ($46) assr asynchronous mode status register $25 ($45) icr1h timer/counter1 input capture register high byte $24 ($44) icr1l timer/counter1 input capture register low byte $23 ($43) tcnt2 timer/counter2 (8-bit) $22 ($42) ocr2 timer/counter2 output compare register $21 ($41) wdtcr watchdog timer control register $20 ($40) ubrrhi uart baud register high $1f ($3f) eearh eeprom address register high $1e ($3e) eearl eeprom address register low $1d ($3d) eedr eeprom data register
atmega161(l) 20 note: reserved and unused locations are not shown in the table. all atmega161 i/os and peripherals are placed in the i/o space. the i/o locations are accessed by the in and out instructions transferring data between the 32 general purpose working registers and the i/o space. i/o registers within the address range $00 - $1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of sin- gle bits can be checked by using the sbis and sbic instructions. refer to the instruction set chapter for more details. when using the i/o specific commands in, out the i/o addresses $00 - $3f must be used. when addressing i/o registers as sram, $20 must be added to this address. all i/o register addresses throughout this document are shown with the sram address in parentheses. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. $1c ($3c) eecr eeprom control register $1b($3b) porta data register, port a $1a ($3a) ddra data direction register, port a $19 ($39) pina input pins, port a $18 ($38) portb data register, port b $17 ($37) ddrb data direction register, port b $16 ($36) pinb input pins, port b $15 ($35) portc data register, port c $14 ($34) ddrc data direction register, port c $13 ($33) pinc input pins, port c $12 ($32) portd data register, port d $11 ($31) ddrd data direction register, port d $10 ($30) pind input pins, port d $0f ($2f) spdr spi i/o data register $0e ($2e) spsr spi status register $0d ($2d) spcr spi control register $0c ($2c) udr0 uart0 i/o data register $0b ($2b) ucsr0a uart0 control and status register $0a ($2a) ucsr0b uart0 control and status register $09 ($29) ubrr0 uart0 baud rate register $08 ($28) acsr analog comparator control and status register $07 ($27) porte data register, port e $06 ($26) ddre data direction register, port e $05 ($25) pine input pins, port e $03 ($23) udr1 uart1 i/o data register $02 ($22) ucsr1a uart1 control and status register $01 ($21) ucsr1b uart1 control and status register $00 ($20) ubrr1 uart1 baud rate register table 1. atmega161 i/o space (continued) i/o address (sram address) name function
atmega161(l) 21 some of the status flags are cleared by writing a logical one to them. note that the cbi and sbi instructions will operate on all bits in the i/o register, writing a one back into any flag read as set, thus clearing the flag. the cbi and sbi instruction s work with registers $00 to $1f only. the i/o and peripherals control registers are explained in the following sections. status register ? sreg the avr status register ? sreg ? at i/o space location $3f ($5f) is defined as:  bit 7 - i: global interrupt enable the global interrupt enable bit must be set (one) for the interrupts to be enabled. the individual interrupt enable control is then performed in separate control registers. if the global interrupt enable bit is cleared (zero), none of the interrupts are enabled independent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interrupts.  bit 6 - t: bit copy storage the bit copy instructions bld (bit load) and bst (bit store) use the t bit as source and destination for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit i n a register in the register file by the bld instruction.  bit 5 - h: half carry flag the half carry flag h indicates a half carry in some arithmetic operations. see the instruction set description for detailed information.  bit 4 - s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two ? s complement overflow flag v. see the instruc- tion set description for detailed information.  bit 3 - v: two ? s complement overflow flag the two ? s complement overflow flag v supports two ? s complement arithmetics. see the instruction set description for detailed information.  bit 2 - n: negative flag the negative flag n indicates a negative result after the different arithmetic and logic operations. see the instruction set description for detailed information.  bit 1 - z: zero flag the zero flag z indicates a zero result after the different arithmetic and logic operations. see the instruction set descriptio n for detailed information.  bit 0 - c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the instruction set description for detailed information. note that the status register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt routine. this must be handled by software. bit 76543210 $3f ($5f) i t h s v n z c sreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 22 stack pointer ? sp the atmega161 stack pointer is implemented as two 8-bit registers in the i/o space locations $3e ($5e) and $3d ($5d). as the atmega161 supports up to 64kb memory, all 16 bits are used. the stack pointer points to the data sram stack area where the subroutine and interrupt stacks are located. this stack space in the data sram must be defined by the program before any subroutine calls are executed or interrupts are enabled. the stack pointer must be set to point above $60. the stack pointer is decremented by one when data is pushed onto the stack with the push instruction, and it is decremented by two when an address is pushed onto the stack with subroutine calls and interrupts. the stack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when an address is popped from the stack with return from subroutine ret or return from interrupt reti. reset and interrupt handling the atmega161 provides 20 different interrupt sources. these interrupts and the separate reset vector, each have a sep- arate program vector in the program memory space. all interrupts are assigned individual enable bits which must be set (one) together with the i-bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are automatically defined as the reset and interrupt vectors. the complete list of vectors is shown in table 2. the list also determines the priority levels of the different interrupts. the low er the address the higher is the priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0 etc. bit 151413121110 9 8 $3e ($5e) sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 sph $3d ($5d) sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000 table 2. reset and interrupt vectors vector no. program address source interrupt definition 1 $000 reset external pin, power-on reset, brown-out reset and watchdog reset 2 $002 int0 external interrupt request 0 3 $004 int1 external interrupt request 1 4 $006 int2 external interrupt request 2 5 $008 timer2 comp timer/counter2 compare match 6 $00a timer2 ovf timer/counter2 overflow 7 $00c timer1 capt timer/counter1 capture event 8 $00e timer1 compa timer/counter1 compare match a 9 $010 timer1 compb timer/counter1 compare match b 10 $012 timer1 ovf timer/counter1 overflow 11 $014 timer0 comp timer/counter0 compare match 12 $016 timer0 ovf timer/counter0 overflow 13 $018 spi, stc serial transfer complete 14 $01a uart0, rx uart0, rx complete 15 $01c uart1, rx uart1, rx complete
atmega161(l) 23 the most typical and general program setup for the reset and interrupt vector addresses are: address labels code comments $000 jmp reset ; reset handler $002 jmp ext_int0 ; irq0 handler $004 jmp ext_int1 ; irq1 handler $006 jmp ext_int2 ; irq2 handler $008 jmp tim2_comp ; timer2 compare handler $00a jmp tim2_ovf ; timer2 overflow handler $00c jmp tim1_capt ; timer1 capture handler $00e jmp tim1_compa ; timer1 comparea handler $010 jmp tim1_compb ; timer1 compareb handler $012 jmp tim1_ovf ; timer1 overflow handler $014 jmp tim0_comp ; timer0 compare handler $016 jmp tim0_ovf ; timer0 overflow handler $018 jmp spi_stc; ; spi transfer complete handler $01a jmp uart_rxc0 ; uart0 rx complete handler $01c jmp uart_rxc1 ; uart1 rx complete handler $01e jmp uart_dre0 ; udr0 empty handler $020 jmp uart_dre1 ; udr1 empty handler $022 jmp uart_txc0 ; uart0 tx complete handler $024 jmp uart_txc1 ; uart1 tx complete handler $026 jmp ee_rdy ; eeprom ready handler $028 jmp ana_comp ; analog comparator handler ; $02a main: ldi r16,high(ramend); main program start $02b out sph,r16 $02c ldi r16,low(ramend) $02d out spl,r16 $02e xxx ? ? ? ? 16 $01e uart0, udre uart0 data register empty 17 $020 uart1, udre uart1 data register empty 18 $022 uart0, tx uart0, tx complete 19 $024 uart1, tx uart1, tx complete 20 $026 ee_rdy eeprom ready 21 $028 ana_comp analog comparator table 2. reset and interrupt vectors (continued) vector no. program address source interrupt definition
atmega161(l) 24 reset sources the atmega161 has four sources of reset:  power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ).  external reset. the mcu is reset when a low level is present on the reset pin for more than 500 ns.  watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled.  brown-out reset. the mcu is reset when the supply voltage v cc falls below a certain voltage. during reset, all i/o registers are then set to their initial values, and the program starts execution from address $000. the instruction placed in address $000 must be an jmp ? relative jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 24 shows the reset logic. table 3 and table 4 defines the timing and electrical parameters of the reset circuitry figure 24. reset logic mcu status register (mcusr) brown-out reset circuit boden bodlevel delay counters cksel[2:0] ck full porf borf extrf wdrf data b u s
atmega161(l) 25 note: the power-on reset will not work unless the supply voltage has been below v pot (falling). ? notes: 1. the cksel fuses control only the start-up time. the oscillator is the same for all selections. on power-up, the real-ti me part of the start-up time is increased with typ. 0.6ms. 2. or external power-on reset. 3. when bod is enabled, there will be a real-time part = 50 ? (typ.) table 4 shows the start-up times from reset. from sleep, only the clock counting part of the start-up time is used. the watchdog oscillator is used for timing the real-time part of the start-up time. the number wdt oscillator cycles used for each time-out is shown in table 5. note: the bod-level fuse can be used to select start-up times even if the brown-out detection is disabled (by leaving the boden fuse unprogrammed). the frequency of the watchdog oscillator is voltage dependent as shown in the electrical characteristics section. the device is shipped with cksel = 010. table 3. reset characteristics (v cc = 5.0v) symbol parameter condition min typ max units v pot power-on reset threshold voltage (rising) bod disabled 1.0 1.4 1.8 v bod enabled 1.7 2.2 2.7 v power-on reset threshold voltage (falling) bod disabled 0.4 0.6 0.8 v bod enabled 1.7 2.2 2.7 v v rst reset pin threshold voltage - - 0.85 v cc v v bot brown-out reset threshold voltage (bodlevel = 1) 2.6 2.7 2.8 v (bodlevel = 0) 3.8 4.0 4.2 table 4. reset delay selections cksel [2:0] start-up time, v cc = 2.7v, bodlevel unprogrammed start-up time, v cc = 4.0v, bodlevel programmed recommended usage (1) 000 4.2 ms + 6 ck 5.8 ms + 6 ck external clock, fast rising power 001 30 ? + 6 ck 10 s + 6 ck external clock, bod enabled (2)(3) 010 67 ms + 16k ck 92 ms + 16k ck crystal oscillator, slowly rising power 011 4.2 ms + 16k ck 5.8 ms + 16k ck crystal oscillator, fast rising power 100 30 ? + 16k ck 10 s + 16k ck crystal oscillator, bod enabled (2)(3) 101 67 ms + 1k ck 92 ms + 1k ck ceramic resonator/external clock, slowly rising power 110 4.2 ms + 1k ck 5.8 ms + 1k ck ceramic resonator, fast rising power 111 30 ? + 1k ck 10 s + 1k ck ceramic resonator, bod enabled (2)(3) table 5. number of watchdog oscillator cycles bodlevel time-out number of cycles unprogrammed 4.2 ms (at v cc =2.7v) 1k unprogrammed 67 ms (at v cc =2.7v) 16k programmed 5.8 ms (at v cc =4.0v) 4k programmed 92 ms (at v cc =4.0v) 64k
atmega161(l) 26 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detection level is nominally 1.4v (rising vcc). the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as detect a failure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reaching the power-on reset threshold voltage invokes a delay counter, which determines the delay, for which the device is kept in reset after v cc rise. the time-out period of the delay counter can be defined by the user through the cksel fuses. the eight different selections for the delay period are presented in table 4. the reset signal is activated again, without any delay, when the v cc decreases below detection level. figure 25. mcu start-up, reset tied to vcc. figure 26. mcu start-up, reset controlled externally external reset an external reset is generated by a low level on the reset pin. reset pulses longer than 500 ns will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst on its positive edge, the delay timer starts the mcu after the time-out period t tout has expired. vcc reset time-out internal reset t tout v pot v rst vcc reset time-out internal reset t tout v pot v rst
atmega161(l) 27 figure 27. external reset during operation brown-out detection atmega161 has an on-chip brown-out detection (bod) circuit for monitoring the v cc level during the operation. the bod circuit can be enabled/disabled by the fuse boden. when boden is enabled (boden programmed), and v cc decreases to a value below the trigger level, the brown-out reset is immediately activated. when v cc increases above the trigger level, the brown-out reset is deactivated after a delay. the delay is defined by the user in the same way as the delay of por sig- nal, in table 4. the trigger level for the bod can be selected by the fuse bodlevel to be 2.7v (bodlevel unprogrammed), or 4.0v ((bodlevel programmed). the trigger level has a hysteresis of 50 mv to ensure spike free brown-out detection. the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for longer than 9 ? for trigger level 4.0v, 21 ? for trigger level 2.7v (typical values). figure 28. brown-out reset during operation watchdog reset when the watchdog times out, it will generate a short reset pulse of 1 xtal cycle duration. on the falling edge of this pulse, the delay timer starts counting the time-out period t tout . refer to page page 52 for details on operation of the watchdog. vcc reset time-out internal reset v bot- v bot+ t tout
atmega161(l) 28 figure 29. watchdog reset during operation mcu status register ? mcusr the mcu status register provides information on which reset source caused an mcu reset.  bits 7..4 - res: reserved bits these bits are reserved bits in the atmega161 and always read as zero.  bit 3 - wdrf: watchdog reset flag this bit is set if a watchdog reset occurs. the bit is cleared by a power-on reset, or by writing a logic zero to the flag.  bit 2 - borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bit is cleared by a power-on reset, or by writing a logic zero to the flag.  bit 1 - extrf: external reset flag this bit is set if an external reset occurs. the bit is cleared by a power-on reset, or by writing a logic zero to the flag.  bit 0 - porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is cleared only by writing a logic zero to the flag. to make use of the reset flags to identify a reset condition, the user should read and then clear the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. interrupt handling the atmega161 has two 8-bit interrupt mask control registers; gimsk ? general interrupt mask register and timsk ? timer/counter interrupt mask register. when an interrupt occurs, the global interrupt enable i-bit is cleared (zero) and all interrupts are disabled. the user soft- ware can set (one) the i-bit to enable nested interrupts. the i-bit is set (one) when a return from interrupt instruction ? reti ? is executed. when the program counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, hard- ware clears the corresponding flag that generated the interrupt. some of the interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs when the corresponding interrupt enable bit is cleared (zero), the interrupt flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software. bit 76543210 $34 ($54) - - - - wdrf borf extrf porf mcusr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
atmega161(l) 29 if one or more interrupt conditions occur when the global interrupt enable bit is cleared (zero), the corresponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set (one), and will be executed by order of priorit y. note that external level interrupt does not have a flag, and will only be remembered for as long as the interrupt condition is present. note that the status register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt routine. this must be handled by software. interrupt response time the interrupt execution response for all the enabled avr interrupts is 4 clock cycles minimum. after 4 clock cycles the program vector address for the actual interrupt handling routine is executed. during this 4 clock cycle period, the program counter (13 bits) is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes 3 clock cycles. if an interrupt occurs during execution of a multi-cycle instruction, this instruction is completed before the interrupt is served. if an interrupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by 4 clock cycles. a return from an interrupt handling routine takes 4 clock cycles. during these 4 clock cycles, the program counter (2 bytes) is popped back from the stack, the stack pointer is incremented by 2, and the i flag in sreg is set. when avr exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served. general interrupt mask register ? gimsk  bit 7 - int1: external interrupt request 1 enable when the int1 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is enabled. the interrupt sense control1 bits 1/0 (isc11 and isc10) in the mcu general control register (mcucr) define whether the external interrupt is activated on rising and/or falling edge of the int1 pin or level sensed. activity on the pin will cau se an interrupt request even if int1 is configured as an output. the corresponding interrupt of external interrupt request 1 is executed from program memory address $004. see also ? external interrupts ? .  bit 6 - int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is enabled. the interrupt sense control0 bits 1/0 (isc01 and isc00) in the mcu general control register (mcucr) defines whether the external interrupt is activated on rising and/or falling edge of the int0 pin or level sensed. activity on the pin will cau se an interrupt request even if int0 is configured as an output. the corresponding interrupt of external interrupt request 0 is executed from program memory address $002. see also ? external interrupts. ?  bit 5- int2: external interrupt request 2 enable when the int2 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is activate d. the interrupt sense control2 bit (isc02 in the extended mcu control register (emcucr) defines whether the external interrupt is activated on rising or falling edge of the int2 pin. activity on the pin will cause an interrupt request even if i nt2 is configured as an output. the corresponding interrupt of external interrupt request 2 is executed from program memory address $006. see also ? external interrupts. ?  bits 4..0 - res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. bit 7 6 5 4 3 2 1 0 $3b ($5b) int1 int0 int2 - - - - - gimsk read/write r/w r/w r r r r r r initial value 0 0 0 0 0 0 0 0
atmega161(l) 30 general interrupt flag register ? gifr  bit 7 - intf1: external interrupt flag1 when an event on the int1 pin triggers an interrupt request, intf1 becomes set (one). if the i-bit in sreg and the int1 bit in gimsk are set (one), the mcu will jump to the interrupt vector at address $004. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it.  bit 6 - intf0: external interrupt flag0 when an event on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gimsk are set (one), the mcu will jump to the interrupt vector at address $002. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it.  bit 5 - intf2: external interrupt flag2 when an event on the int2 pin triggers an interrupt request, intf2 becomes set (one). if the i-bit in sreg and the int2 bit in gimsk are set (one), the mcu will jump to the interrupt vector at address $006. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it.  bits 4..0 - res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. timer/counter interrupt mask register ? timsk  bit 7 - toie1: timer/counter1 overflow interrupt enable when the toie1 bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 overflow interrupt is enabled. the corresponding interrupt (at vector $012) is executed if an overflow in timer/counter1 occurs, i.e., when the tov1 bit is set in the timer/counter interrupt flag register ? tifr.  bit 6 - oce1a:timer/counter1 output comparea match interrupt enable when the ocie1a bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 comparea match interrupt is enabled. the corresponding interrupt (at vector $00e) is executed if a comparea match in timer/counter1 occurs, i.e., when the ocf1a bit is set in the timer/counter interrupt flag register ? tifr.  bit 5 - ocie1b:timer/counter1 output compareb match interrupt enable when the ocie1b bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 compareb match interrupt is enabled. the corresponding interrupt (at vector $010) is executed if a compareb match in timer/counter1 occurs, i.e., when the ocf1b bit is set in the timer/counter interrupt flag register ? tifr.  bit 4 - toie2: timer/counter2 overflow interrupt enable when the toie2 bit is set (one) and the i-bit in the status register is set (one), the timer/counter2 overflow interrupt is enabled. the corresponding interrupt (at vector $00a) is executed if an overflow in timer/counter2 occurs, i.e., when the tov2 bit is set in the timer/counter interrupt flag register ? tifr.  bit 3 - ticie1: timer/counter1 input capture interrupt enable when the ticie1 bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 input capture event interrupt is enabled. the corresponding interrupt (at vector $00c) is executed if a capture-triggering event occurs on pin 31, icp, i.e., when the icf1 bit is set in the timer/counter interrupt flag register ? tifr.  bit 2 - ocie2:timer/counter2 output compare match interrupt enable when the ocie2 bit is set (one) and the i-bit in the status register is set (one), the timer/counter2 compare match inter- rupt is enabled. the corresponding interrupt (at vector $008) is executed if a compare2 match in timer/counter2 occurs, i.e., when the ocf2 bit is set in the timer/counter interrupt flag register ? tifr. bit 7 6 5 4 3 2 1 0 $3a ($5a) intf1 intf0 intf2 - - - - - gifr read/write r/w r/w r/w r r r r r initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $39 ($59) toie1 ocie1a ocie1b toie2 ticie1 ocie2 toie0 ocie0 timsk read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 31  bit 1 - toie0: timer/counter0 overflow interrupt enable when the toie0 bit is set (one) and the i-bit in the status register is set (one), the timer/counter0 overflow interrupt is enabled. the corresponding interrupt (at vector $016) is executed if an overflow in timer/counter0 occurs, i.e., when the tov0 bit is set in the timer/counter interrupt flag register ? tifr.  bit 0 - ocie0: timer/counter0 output compare match interrupt enable when the ocie0 bit is set (one) and the i-bit in the status register is set (one), the timer/counter0 compare match inter- rupt is enabled. the corresponding interrupt (at vector $014) is executed if a compare0 match in timer/counter0 occurs, i.e., when the ocf0 bit is set in the timer/counter interrupt flag register ? tifr. timer/counter interrupt flag register ? tifr  bit 7 - tov1: timer/counter1 overflow flag the tov1 is set (one) when an overflow occurs in timer/counter1. tov1 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov1 is cleared by writing a logic one to the flag. when the i-bit in sreg, and toie1 (timer/counter1 overflow interrupt enable), and tov1 are set (one), the timer/counter1 overflow interrupt is executed. in pwm mode, this bit is set when timer/counter1 changes counting direction at $0000.  bit 6 - ocf1a: output compare flag 1a the ocf1a bit is set (one) when compare match occurs between the timer/counter1 and the data in ocr1a ? output compare register 1a. ocf1a is cleared by hardware when executing the corresponding interrupt handling vector. alterna- tively, ocf1a is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie1a (timer/counter1 compare match interrupta enable), and the ocf1a are set (one), the timer/counter1 compare a match interrupt is executed.  bit 5 - ocf1b: output compare flag 1b the ocf1b bit is set (one) when compare match occurs between the timer/counter1 and the data in ocr1b ? output compare register 1b. ocf1b is cleared by hardware when executing the corresponding interrupt handling vector. alterna- tively, ocf1b is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie1b (timer/counter1 compare match interruptb enable), and the ocf1b are set (one), the timer/counter1 compare b match interrupt is executed.  bit 4 - tov2: timer/counter2 overflow flag the bit tov2 is set (one) when an overflow occurs in timer/counter2. tov2 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov2 is cleared by writing a logic one to the flag. when the sreg i-bit, and toie2 (timer/counter2 overflow interrupt enable), and tov2 are set (one), the timer/counter2 overflow inter- rupt is executed.  bit 3 - icf1: - input capture flag 1 the icf1 bit is set (one) to flag an input capture event, indicating that the timer/counter1 value has been transferred to the input capture register ? icr1. icf1 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, icf1 is cleared by writing a logic one to the flag. when the sreg i-bit, and ticie1 (timer/counter1 input capture interrupt enable), and icf1 are set (one), the timer/counter1 capture interrupt is executed.  bit 2 - ocf2: output compare flag 2 the ocf2 bit is set (one) when compare match occurs between the timer/counter2 and the data in ocr2 ? output com- pare register 2. ocf2 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf2 is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie2 (timer/counter2 compare match interrupta enable), and the ocf2 are set (one), the timer/counter2 compare match interrupt is executed.  bit 1 - tov0: timer/counter0 overflow flag the bit tov0 is set (one) when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bit, and toie0 (timer/counter0 overflow interrupt enable), and tov0 are set (one), the timer/counter0 overflow interrupt is executed. bit 7 6 5 4 3 2 1 0 $38 ($58) tov1 ocf1a ocifb tov2 icf1 ocf2 tov0 ocf0 tifr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 32  bit 2 - ocf0: output compare flag 0 the ocf0 bit is set (one) when compare match occurs between the timer/counter0 and the data in ocr0 ? output com- pare register 0. ocf0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf0 is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie0 (timer/counter0 compare match interrupta enable), and the ocf0 are set (one), the timer/counter0 compare match interrupt is executed. external interrupts the external interrupts are triggered by the int0, int1 and int2 pins. observe that, if enabled, the interrupts will trigger even if the int0/int1/int2 pins are configured as outputs. this feature provides a way of generating a software interrupt. the external interrupts can be triggered by a falling or rising edge or a low level (int2 is only an edge triggered interrupt). this is set up as indicated in the specification for the mcu control register ? mcucr (int0/int1) and emcucr (int2). when the external interrupt is enabled and is configured as level triggered (only int0/int1), the interrupt will trigger as lon g as the pin is held low. mcu control register ? mcucr the mcu control register contains control bits for general mcu functions.  bit 7 - sre: external sram enable when the sre bit is set (one), the external data memory interface is enabled, and the pin functions ad0-7 (port a), a8-15 (port c), ale (port e), wr and rd (port d) are activated as the alternate pin functions. the sre bit overrides any pin direction settings in the respective data direction registers. see figure 51 ? figure 54 for a description of the external mem- ory pin functions. when the sre bit is cleared (zero), the external data memory interface is disabled, and the normal pin and data direction settings are used.  bit 6 - srw10: external sram wait state the srw10 bit is used to set up extra wait states in the external memory interface. see ? interface to external memory ? on page 72 for a detailed description.  bit 5 - se: sleep enable the se bit must be set (one) to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmers purpose, it is recommended to set the sleep enable se bit just before the execution of the sleep instruction.  bit 4 - sm1: sleep mode select bit 1 the sm1 bit together with the sm0 control bit in emcucr selects between the three available sleep modes as shown in the following table.  bits 3, 2 - isc11, isc10: interrupt sense control 1 bit 1 and bit 0 the external interrupt 1 is activated by the external pin int1 if the sreg i-flag and the corresponding interrupt mask in the gimsk are set. the level and edges on the external int1 pin that activate the interrupt are defined in table 7. the value on the int1 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. bit 76543210 $35 ($55) sre srw10 se sm1 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 6. sleep mode select sm1 sm0 sleep mode 0 0 idle mode 01reserved 1 0 power-down 1 1 power save
atmega161(l) 33 note: when changing the isc11/isc10 bits, int1 must be disabled by clearing its interrupt enable bit in the gimsk register. othe r- wise an interrupt can occur when the bits are changed.  bit 1, 0 - isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the external pin int0 if the sreg i-flag and the corresponding interrupt mask is set. the level and edges on the external int0 pin that activate the interrupt are defined in table 8. the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. note: when changing the isc01/isc00 bits, int0 must be disabled by clearing its interrupt enable bit in the gimsk register. othe r- wise an interrupt can occur when the bits are changed. extended mcu control register ? emcucr the extended mcu control register contains control bits for external interrupt 2, sleep mode bit and control bits for the external memory interface.  bit 7 - sm0: sleep mode bit 0 when this bit is set (one) and sleep mode bit 1 (sm1) in mcucr is set, power save mode is selected as sleep mode. refer to page 34 for a detailed description of the sleep modes.  bit 6..4 - srl2, srl1, srl0: external sram limit it is possible to configure different wait-states for different external memory addresses in atmega161. the srl2 ? srl0 bits are used to define at which address the different wait-states will be configured. see ? interface to external memory ? on page 72 for a detailed description.  bit 3..1 - srw01, srw00, srw11: external sram wait-state select bits. the srw01, srw00 and srw11 bits are used to set up extra wait states in the external memory interface. see ? interface to external memory ? on page 72 for a detailed description.  bit 0 - isc2: interrupt sense control 2 the external interrupt 2 is activated by the external pin int2 if the sreg i-flag and the corresponding interrupt mask in the gimsk are set. if isc2 is cleared (zero) a falling edge on int2 activates the interrupt. if isc2 is set (one) a rising edge on int2 activates the interrupt. edges on int2 are registered asynchronously. pulses on int2 wider than 50 ns will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. table 7. interrupt 1 sense control isc11 isc10 description 0 0 the low level of int1 generates an interrupt request. 0 1 any logical change on int1 generates an interrupt request 1 0 the falling edge of int1 generates an interrupt request. 1 1 the rising edge of int1 generates an interrupt request. table 8. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request. bit 76543210 $36 ($56) sm0 srl2 srl1 srl0 srw01 srw00 srw11 isc2 emcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 34 sleep modes to enter any of the three sleep modes, the se bit in mcucr must be set (one) and a sleep instruction must be executed. the sm1 bit in the mcucr register and sm0 bit in the emcucr register select which sleep mode (idle, power-down, or power save) will be activated by the sleep instruction, see table 6. if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu awakes. the cpu is then halted for 4 cycles, it executes the interrupt routine, and resumes execu- tion from the instruction following sleep. the contents of the register file, sram, and i/o memory are unaltered. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector idle mode when the sm1/sm0 bits are set to 00, the sleep instruction makes the mcu enter the idle mode, stopping the cpu but allowing spi, uarts, analog comparator, timer/counters, watchdog and the interrupt system to continue operating. this enables the mcu to wake-up from external triggered interrupts as well as internal ones like the timer overflow and uart receive complete interrupts. if wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd-bit in the analog comparator control and status register ? acsr. this will reduce power consumption in idle mode. power-down mode when the sm1/sm0 bits are set to 10, the sleep instruction makes the mcu enter the power-down mode. in this mode, the external oscillator is stopped, while the external interrupts and the watchdog (if enabled) continue operating. only an external reset, a watchdog reset (if enabled), an external level interrupt on int0 or int1, or an external edge interrupt on int2 can wake-up the mcu. if int2 is used for wake-up from power-down mode, the edge is remembered until the mcu wakes up. if a level triggered interrupt is used for wake-up from power-down mode, the changed level must be held for some time to wake-up the mcu. this makes the mcu less sensitive to noise. the changed level is sampled twice by the watchdog oscil- lator clock, and if the input has the required level during this time, the mcu will wake-up. the period of the watchdog oscillator is 1 ? (nominal) at 5.0v and 25c. the frequency of the watchdog oscillator is voltage dependent as shown in the electrical characteristics section. when waking up from power-down mode, there is a delay from the wake-up condition occurs until the wake-up becomes effective. this allows the clock to restart and become stable after having been stopped. the wake-up period is defined by the same cksel fuses that define the reset time-out period. the wake-up period is equal to the clock counting part of the reset period, as shown in table 4. if the wake-up condition disappears before the mcu wakes up and starts to execute, e.g. a low level on int0 is not held long enough, the interrupt causing the wake-up will not be executed. power save mode when the sm1/sm0 bits are 11, the sleep instruction makes the mcu enter the power save mode. this mode is identical to power-down, with one exception: if timer/counter2 is clocked asynchronously, i.e. the as2 bit in assr is set, timer/counter2 will run during sleep. in addi- tion to the power-down wake-up sources, the device can also wake-up from either timer overflow or output compare event from timer/counter2 if the corresponding timer/counter2 interrupt enable bits are set in timsk and the global inter- rupt enable bit in sreg is set. timer/counters the atmega161 provides three general purpose timer/counters ? two 8-bit t/cs and one 16-bit t/c. timer/counter2 can optionally be asynchronously clocked from an external oscillator. this oscillator is optimized for use with a 32.768 khz watch crystal, enabling use of timer/counter2 as a real time clock (rtc). timer/counters 0 and 1 have individual pres- caling selection from the same 10-bit prescaling timer. timer/counter2 has its own prescaler. both these prescalers can be reset by setting the corresponding control bits in the special functions io register (sfior). refer to page 36 for a detailed description. these timer/counters can either be used as a timer with an internal clock time-base or as a counter with an external pin connection which triggers the counting.
atmega161(l) 35 timer/counter prescalers figure 30. prescaler for timer/counter0 and 1 for timer/counters 0 and 1, the four prescaled selections are: ck/8, ck/64, ck/256 and ck/1024, where ck is the oscilla- tor clock. for the two timer/counters 0 and 1, ck, external source, and stop, can also be selected as clock sources. setting the psr10 bit in sfior resets the prescaler. this allows the user to operate with a predictable prescaler. note that timer/counter1 and timer/counter 0 share the same prescaler and a prescaler reset will affect both timer/counters. psr10 clear tck1 tck0
atmega161(l) 36 figure 31. timer/counter2 prescaler the clock source for timer/counter2 prescaler is named pck2. pck2 is by default connected to the main system clock ck. by setting the as2 bit in assr, timer/counter2 is asynchronously clocked from the pd4(tosc1) pin. this enables use of timer/counter2 as a real time clock (rtc). when as2 is set, pins pd4(tosc1) and pd5(tosc2) are discon- nected from port d. a crystal can then be connected between the pd4(tosc1) and pd5(tosc2) pins to serve as an independent clock source for timer/counter2. the oscillator is optimized for use with a 32.768 khz crystal. alternatively, an external clock signal can be applied to pd4(tosc1). the frequency of this clock must be lower than one fourth of the cpu clock and not higher than 256 khz. setting the psr2 bit in sfior resets the prescaler. this allows the user to operate with a predictable prescaler. special function io register ? sfior  bit 7..2 - res: reserved bits these bits are reserved bits in the atmega161 and always read as zero.  bit 1 - psr2: prescaler reset timer/counter2 when this bit is set (one) the timer/counter2 prescaler will be reset. the bit will be cleared by hardware after the operation is performed. writing a zero to this bit will have no effect. this bit will always be read as zero if timer/counter2 is clocked by the internal cpu clock. if this bit is written when timer/counter2 is operating in asynchronous mode however, the bit will remain as one until the prescaler has been reset. see ? asynchronous operation of timer/counter2 ? on page 43 for a detailed description of asynchronous operation.  bit 0 - psr10: prescaler reset timer/counter1 and timer/counter0 when this bit is set (one) the timer/counter1 and timer/counter0 prescaler will be reset. the bit will be cleared by hard- ware after the operation is performed. writing a zero to this bit will have no effect. note that timer/counter1 and timer/counter0 share the same prescaler and a reset of this prescaler will affect both timers. this bit will always be read as zero. bit 7 6 5 4 3 2 1 0 $30 ($50) - - - - - - psr2 psr10 sfior read/write r r r r r r r/w r/w initial value 0 0 0 0 0 0 0 0 10-bit t/c prescaler timer/counter2 clock source ck pck2 tosc1 as2 cs20 cs21 cs22 pck2/8 pck2/64 pck2/128 pck2/1024 pck2/256 pck2/32 0 psr2 clear tck2
atmega161(l) 37 8-bit timers/counters t/c0 and t/c2 figure 32 shows the block diagram for timer/counter0. figure 33 shows the block diagram for timer/counter2. figure 32. timer/counter0 block diagram figure 33. timer/counter2 block diagram 8-bit data bus timer int. flag register (tifr) timer/counter0 (tcnt0) 8-bit comparator output compare register0 (ocr0) timer int. mask register (timsk) 0 0 0 7 7 7 t/c clk source up/down t/c clear control logic tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf0 tov0 ck t/c0 over- flow irq t/c0 compare match irq ocf0 tov0 toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocie0 t/c0 control register (tccr0) cs02 com01 pwm0 cs01 com00 cs00 ctc0 foc0 psr2 psr10 special functions io register (sfior) t0 8-bit data bus 8-bit asynch t/c2 data bus asynch. status register (assr) timer int. flag register (tifr) timer/counter2 (tcnt2) synch unit 8-bit comparator output compare register2 (ocr2) timer int. mask register (timsk) 0 0 0 7 7 7 t/c clk source up/down t/c clear control logic ocf2 tov2 toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocr2ub tc2ub icr2ub tosc1 ck tck2 t/c2 over- flow irq t/c2 compare match irq as2 tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf0 tov0 ocie0 t/c2 control register (tccr2) cs22 com21 pwm2 cs21 com20 cs20 ctc2 foc2 psr2 psr10 special functions io register (sfior) ck
atmega161(l) 38 the 8-bit timer/counter0 can select clock source from ck, prescaled ck, or an external pin. the 8-bit timer/counter2 can select clock source from ck, prescaled ck or external tosc1. both timers/counters can be stopped as described in section ? timer/counter0 control register ? tccr0 ? on page 38 and ? timer/counter2 control register ? tccr2 ? on page 38 the various status flags (overflow and compare match) are found in the timer/counter interrupt flag register ? tifr. con- trol signals are found in the timer/counter control register ? tccr0 and tccr2. the interrupt enable/disable settings are found in the timer/counter interrupt mask register ? timsk. when timer/counter0 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. the 8-bit timer/counters feature both a high resolution and a high accuracy usage with the lower prescaling opportunities. similarly, the high prescaling opportunities make the timer/counter0 useful for lower speed functions or exact timing func- tions with infrequent actions. timer/counter0 and 2 can also be used as 8-bit pulse width modulators. in this mode, the timer/counter and the output compare register serve as a glitch-free, stand-alone pwm with centered pulses. refer to page 41 for a detailed description on this function. timer/counter0 control register ? tccr0 timer/counter2 control register ? tccr2  bit 7 - foc0/foc2: force output compare writing a logical one to this bit, forces a change in the compare match output pin pb0 (timer/counter0) and pb1 (timer/counter2) according to the values already set in comn1 and comn0. if the comn1 and comn0 bits are written in the same cycle as foc0/foc2, the new settings will not take effect until next compare match or forced output compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in comn1 and comn0 happens as if a compare match had occurred, but no interrupt is generated and the timer/counters will not be cleared even if ctc0/ctc2 is set. the foc0/foc2 bits will always be read as zero. the setting of the foc0/foc2 bits has no effect in pwm mode.  bit 6 - pwm0/pwm2: pulse width modulator enable when set (one) this bit enables pwm mode for timer/counter0 or timer/counter2. this mode is described on page 41.  bits 5,4 - com01, com00/com21, com20: compare output mode, bits 1 and 0 the comn1 and comn0 control bits determine any output pin action following a compare match in timer/counter0 or timer/counter2. output pin actions affect pins pb0(oc0) or pb1(oc2). this is an alternative function to an i/o port, and the corresponding direction control bit must be set (one) to control an output pin. the control configuration is shown in table 9. bit 7 6 5 4 3 2 1 0 $33 ($53) foc0 pwm0 com01 com00 ctc0 cs02 cs01 cs00 tccr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $27 ($47) foc2 pwm2 com21 com20 ctc2 cs22 cs21 cs20 tccr2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 39 notes: 1. in pwm mode, these bits have a different function. refer to table 12 for a detailed description. 2. n = 0 or 2  bit 3 - ctc0/ctc2: clear timer/counter on compare match when the ctc0 or ctc2 control bit is set (one), timer/counter0 or timer/counter2 is reset to $00 in the cpu clock cycle after a compare match. if the control bit is cleared, timer/counter continues counting and is unaffected by a compare match. when a prescaling of 1 is used, and the compare register is set to c, the timer will count as follows if ctc0/ctc2 is set: ... | c-1 | c | 0 | 1 | ... when the prescaler is set to divide by 8, the timer will count like this: ... | c-1, c-1, c-1, c-1, c-1, c-1, c-1, c-1 | c, c, c, c, c, c, c, c | 0, 0, 0, 0, 0, 0, 0, 0 | 1, 1, 1, ... in pwm mode, this bit has a different function. if the ctc0 or ctc2 bit is cleared in pwm mode, the timer/counter acts as an up/down counter. if the ctc0 or ctc2 bit is set (one), the timer/counter wraps when it reaches $ff. refer to page 41 for a detailed description.  bits 2,1,0 - cs02, cs01, cs00/ cs22, cs21, cs20: clock select bits 2,1 and 0 the clock select bits 2,1 and 0 define the prescaling source of timer/counter0 and timer/counter2. table 9. compare mode select comn1 comn0 description 0 0 timer/counter disconnected from output pin ocn 0 1 toggle the ocn output line. 1 0 clear the ocn output line (to zero). 1 1 set the ocn output line (to one). table 10. clock 0 prescale select cs02 cs01 cs00 description 0 0 0 stop, the timer/counter0 is stopped. 001ck 010ck/8 011ck/64 100ck/256 101ck/1024 1 1 0 external pin pb0(t0), falling edge 1 1 1 external pin pb0(t0), rising edge
atmega161(l) 40 the stop condition provides a timer enable/disable function. the prescaled modes are scaled directly from the ck oscilla- tor clock for timer/counter0 and pck2 for timer/counter2. if the external pin modes are used for timer/counter0, transitions on pb0/(t0) will clock the counter even if the pin is configured as an output. this feature can give the user sw control of the counting. timer counter0 ? tcnt0 timer/counter2 ? tcnt2 these 8-bit registers contain the value of the timer/counters. both timer/counters is realized as up or up/down (in pwm mode) counters with read and write access. if the timer/counter is written to and a clock source is selected, it continues counting in the timer clock cycle following the write operation. timer/counter0 output compare register ? ocr0 timer/counter2 output compare register ? ocr2 table 11. clock 2 prescale select cs22 cs21 cs20 description 0 0 0 stop, the timer/counter2 is stopped. 0 0 1 pck2 0 1 0 pck2/8 0 1 1 pck2/32 1 0 0 pck2/64 1 0 1 pck2/128 1 1 0 pck2/256 1 1 1 pck2/1024 bit 76543210 $32 ($52) msb lsb tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $23 ($43) msb lsb tcnt2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $31 ($51) msb lsb ocr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $22 ($42) msb lsb ocr2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
atmega161(l) 41 the output compare registers are 8-bit read/write registers. the timer/counter output compare registers contains the data to be continuously compared with the timer/counter. actions on compare matches are specified in tccr0 and tccr2. a compare match does only occur if the timer/counter counts to the ocr value. a software write that sets timer/counter and output compare register to the same value does not generate a compare match. a compare match will set the compare interrupt flag in the cpu clock cycle following the compare event. timer/counter 0 and 2 in pwm mode when pwm mode is selected, the timer/counter either wraps (overflows) when it reaches $ff or it acts as an up/down counter. if the up/down mode is selected, the timer/counter and the output compare registers ? ocr0 or ocr2 form an 8-bit, free-running, glitch-free and phase correct pwm with outputs on the pb0(oc0/pwm0) or pb1(oc2/pwm2) pin. if the overflow mode is selected, the timer/counter and the output compare registers ? ocr0 or ocr2 form an 8-bit, free-running and glitch-free pwm, operating with twice the speed of the up/down counting mode. pwm modes (up/down and overflow) the two different pwm modes are selected by the ctc0 or ctc2 bit in the timer/counter control registers ? tccr0 or tccr2 respectively. if ctc0/ctc2 is cleared and pwm mode is selected, the timer/counter acts as an up/down counter, counting up from $00 to $ff, where it turns and counts down again to zero before the cycle is repeated. when the counter value matches the contents of the output compare register, the pb0(oc0/pwm0) or pb1(oc2/pwm2) pin is set or cleared according to the settings of the comn1/comn0 bits in the timer/counter control registers tccr0 or tccr2. if ctc0/ctc2 is set and pwm mode is selected, the timer/counters will wrap and start counting from $00 after reaching $ff. the pb0(oc0/pwm0) or pb1(oc2/pwm2) pin will be set or cleared according to the settings of comn1/comn0 on a timer/counter overflow or when the counter value matches the contents of the output compare register. refer to table 12 for details. note: n = 0 or 2 note that in pwm mode, the value to be written to the output compare register is first transferred to a temporary location, and then latched into the ocr when the timer/counter reaches $ff. this prevents the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr0 or ocr2 write. see figure 34 and figure 35 for examples. table 12. compare mode select in pwm mode ctcn comn1 comn0 effect on compare pin frequency 0 0 0 not connected 0 0 1 not connected 01 0 cleared on compare match, up-counting. set on compare match, down-counting (non-inverted pwm). f tck0/2 /510 01 1 cleared on compare match, down-counting. set on compare match, up-counting (inverted pwm). f tck0/2 /510 1 0 0 not connected 1 0 1 not connected 1 1 0 cleared on compare match, set on overflow. f tck0/2 /256 1 1 1 set on compare match, cleared on overflow. f tck0/2 /256
atmega161(l) 42 figure 34. effects of unsynchronized ocr latching in up/down mode figure 35. effects of unsynchronized ocr latching in overflow mode. note: n = 0 or 2 (figure 34 and figure 35) during the time between the write and the latch operation, a read from the output compare registers will read the contents of the temporary location. this means that the most recently written value always will read out of ocr0 and ocr2. when the output compare register contains $00 or $ff, and the up/down pwm mode is selected, the output pb0(oc0/pwm0)/pb1(oc2/pwm2) is updated to low or high on the next compare match according to the settings of comn1/comn0. this is shown in table 13. in overflow pwm mode, the output pb0(oc0/pwm0)/pb1(oc2/pwm2) is held low or high only when the output compare register contains $ff. note: n = 0 or 2 in overflow pwm mode, the table above is only valid for ocrn = $ff. table 13. pwm outputs ocrn = $00 or $ff comn1 comn0 ocrn output pwmn 10$00 l 10$ff h 11$00 h 11$ff l pwm output ocn pwm output ocn unsynchronized ocn latch synchronized ocn latch compare value changes counter value compare value glitch counter value compare value compare value changes pwm output ocn pwm output ocn unsynchronized ocn latch synchronized ocn latch counter value compare value counter value compare value compare value changes compare value changes glitch
atmega161(l) 43 in up/down pwm mode, the timer overflow flag, tov0 or tov2, is set when the counter advances from $00. in overflow pwm mode, the timer overflow flag is set as in normal timer/counter mode. timer overflow interrupt0 and 2 operate exactly as in normal timer/counter mode, i.e. they are executed when tov0 or tov2 are set provided that timer overflow interrupt and global interrupts are enabled. this does also apply to the timer output compare flag and interrupt. asynchronous status register ? assr  bit 7..4 - res: reserved bits these bits are reserved bits in the atmega161 and always read as zero.  bit 3 - as2: asynchronous timer/counter2 mode when this bit is cleared (zero) timer/counter2 is clocked from the internal system clock, ck. if as2 is set, the timer/counter2 is clocked from the tosc1 pin. pins pd4 and pd5 become connected to a crystal oscillator and cannot be used as general i/o pins. when the value of this bit is changed the contents of tcnt2, ocr2 and tccr2 might get corrupted.  bit 2 - tcn2ub: timer/counter2 update busy when timer/counter2 operates asynchronously and tcnt2 is written, this bit becomes set (one). when tcnt2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical zero in this bit indicates that tcnt2 is ready to be updated with a new value.  bit 1 - ocr2ub: output compare register2 update busy when timer/counter2 operates asynchronously and ocr2 is written, this bit becomes set (one). when ocr2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical zero in this bit indicates that ocr2 is ready to be updated with a new value.  bit 0 - tcr2ub: timer/counter control register2 update busy when timer/counter2 operates asynchronously and tccr2 is written, this bit becomes set (one). when tccr2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical zero in this bit indicates that tccr2 is ready to be updated with a new value. if a write is performed to any of the three timer/counter2 registers while its update busy flag is set (one), the updated value might get corrupted and cause an unintentional interrupt to occur. the mechanisms for reading tcnt2, ocr2, and tccr2 are different. when reading tcnt2, the actual timer value is read. when reading ocr2 or tccr2, the value in the temporary storage register is read. asynchronous operation of timer/counter2 when timer/counter2 operates asynchronously, some considerations must be taken.  warning: when switching between asynchronous and synchronous clocking of timer/counter2, the timer registers; tcnt2, ocr2 and tccr2 might get corrupted. a safe procedure for switching clock source is: 1. disable the timer/counter2 interrupts by clearing ocie2 and toie2. 2. select clock source by setting as2 as appropriate. 3. write new values to tcnt2, ocr2, and tccr2. 4. to switch to asynchronous operation: wait for tcn2ub, ocr2ub, and tcr2ub. 5. enable interrupts, if needed.  the oscillator is optimized for use with a 32,768 hz watch crystal. an external clock signal applied to this pin goes through the same amplifier having a bandwidth of 256 khz. the external clock signal should therefore be in the interval 0 hz - 256 khz. the frequency of the clock signal applied to the tosc1 pin must be lower than one fourth of the cpu main clock frequency. bit 76543 2 1 0 $26 ($46) - - - - as2 tcn2ub ocr2ub tcr2ub assr read/write r r r r r/w r r r initial value 0 0 0 0 0 0 0 0
atmega161(l) 44  when writing to one of the registers tcnt2, ocr2, or tccr2, the value is transferred to a temporary register, and latched after two positive edges on tosc1. the user should not write a new value before the contents of the temporary register have been transferred to its destination. each of the three mentioned registers have their individual temporary register, which means that e.g. writing to tcnt2 does not disturb an ocr2 write in progress. to detect that a transfer to the destination register has taken place, a asynchronous status register ? assr has been implemented.  when entering power save mode after having written to tcnt2, ocr2, or tccr2, the user must wait until the written register has been updated if timer/counter2 is used to wake-up the device. otherwise, the mcu will go to sleep before the changes have had any effect. this is extremely important if the output compare2 interrupt is used to wake-up the device; output compare is disabled during write to ocr2 or tcnt2. if the write cycle is not finished (i.e. the mcu enters sleep mode before the ocr2ub bit returns to zero), the device will never get a compare match and the mcu will not wake-up.  if timer/counter2 is used to wake-up the device from power save mode, precautions must be taken if the user wants to re-enter power save mode: the interrupt logic needs one tosc1 cycle to be reset. if the time between wake-up and re-entering power save mode is less than one tosc1 cycle, the interrupt will not occur and the device will fail to wake-up. if the user is in doubt whether the time before re-entering power save is sufficient, the following algorithm can be used to ensure that one tosc1 cycle has elapsed: 1. write a value to tccr2, tcnt2, or ocr2 2. wait until the corresponding update busy flag in assr returns to zero. 3. enter power save mode  when asynchronous operation is selected, the 32 khz oscillator for timer/counter2 is always running, except in power- down mode. after a power-up reset or wake-up from power-down, the user should be aware of the fact that this oscillator might take as long as one second to stabilize. therefore, the contents of all timer2 registers must be considered lost after a wake-up from power-down, due to the unstable clock signal. the user is advised to wait for at least one second before using timer/counter2 after power-up or wake-up from power-down.  description of wake-up from power save mode when the timer is clocked asynchronously: when the interrupt condition is met, the wake-up process is started on the following cycle of the timer clock, that is, the timer is always advanced by at least one before the processor can read the counter value. the interrupt flags are updated 3 processor cycles after the processor clock has started. during these cycles, the processor executes instructions, but the interrupt condition is not readable, and the interrupt routine has not started yet.  during asynchronous operation, the synchronization of the interrupt flags for the asynchronous timer takes 3 processor cycles plus one timer cycle. the timer is therefore advanced by at least one before the processor can read the timer value causing the setting of the interrupt flag. the output compare pin is changed on the timer clock and is not synchronized to the processor clock.
atmega161(l) 45 timer/counter1. figure 36 shows the block diagram for timer/counter1. figure 36. timer/counter1 block diagram the 16-bit timer/counter1 can select clock source from ck, prescaled ck, or an external pin. in addition it can be stopped as described in section ? timer/counter1 control register b ? tccr1b ? on page 47. the different status flags (overflow, compare match and capture event) are found in the timer/counter interrupt flag register ? tifr. control signals are found in the timer/counter1 control registers ? tccr1a and tccr1b. the interrupt enable/disable settings for timer/counter1 are found in the timer/counter interrupt mask register ? timsk. when timer/counter1 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. the 16-bit timer/counter1 features both a high resolution and a high accuracy usage with the lower prescaling opportuni- ties. similarly, the high prescaling opportunities makes the timer/counter1 useful for lower speed functions or exact timing functions with infrequent actions. toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocie0 tov0 tov1 ocf1a ocf1b icf1 tov2 ocf2 ocf0 timer int. flag register (tifr) control logic timer/counter1 (tcnt1) timer int. mask register (timsk) t/c1 input capture register (icr1) t/c1 over- flow irq t/c1 compare matcha irq t/c1 compare matchb irq t/c1 input capture irq 8-bit data bus 16 bit comparator timer/counter1 output compare register a 16 bit comparator timer/counter1 output compare register b capture trigger ck 0 7 8 15 0 7 8 15 0 7 8 15 0 7 8 15 0 7 8 15 0 7 8 15 t/c clear t/c clock source up/down t/c1 control register a (tccr1a) t/c1 control register b (tccr1b) foc1a foc1b com1a1 com1a0 com1b1 com1b0 pwm11 pwm10 icnc1 ices1 ctc1 cs12 cs11 cs10 psr2 psr10 special functions io register (sfior) tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf0 tov0 t1
atmega161(l) 46 the timer/counter1 supports two output compare functions using the output compare register 1 a and b ? ocr1a and ocr1b as the data sources to be compared to the timer/counter1 contents. the output compare functions include optional clearing of the counter on comparea match, and actions on the output compare pins on both compare matches. timer/counter1 can also be used as a 8-, 9- or 10-bit pulse with modulator. in this mode the counter and the ocr1a/ocr1b registers serve as a dual glitch-free stand-alone pwm with centered pulses. alternatively, the timer/counter1 can be configured to operate at twice the speed in pwm mode, but without centered pulses. refer to page 50 for a detailed description on this function. the input capture function of timer/counter1 provides a capture of the timer/counter1 contents to the input capture register ? icr1, triggered by an external event on the input capture pin ? icp. the actual capture event settings are defined by the timer/counter1 control register ? tccr1b. in addition, the analog comparator can be set to trigger the input capture. refer to the section, ? the analog comparator ? , for details on this. the icp pin logic is shown in figure 37. figure 37. icp pin schematic diagram if the noise canceler function is enabled, the actual trigger condition for the capture event is monitored over 4 samples, and all 4 must be equal to activate the capture flag. timer/counter1 control register a ? tccr1a  bits 7,6 - com1a1, com1a0: compare output mode1a, bits 1 and 0 the com1a1 and com1a0 control bits determine any output pin action following a compare match in timer/counter1. any output pin actions affect pin oc1a ? output comparea pin 1. this is an alternative function to an i/o port, and the cor- responding direction control bit must be set (one) to control an output pin. the control configuration is shown in table 14.  bits 5,4 - com1b1, com1b0: compare output mode1b, bits 1 and 0 the com1b1 and com1b0 control bits determine any output pin action following a compare match in timer/counter1. any output pin actions affect pin oc1b ? output compareb. this is an alternative function to an i/o port, and the corre- sponding direction control bit must be set (one) to control an output pin. the following control configuration is given: x = a or b in pwm mode, these bits have a different function. refer to table 18 for a detailed description. bit 7 6 5 4 3 2 1 0 $2f ($4f) com1a1 com1a0 com1b1 com1b0 foc1a foc1b pwm11 pwm10 tccr1a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 14. compare 1 mode select com1x1 com1x0 description 0 0 timer/counter1 disconnected from output pin oc1x 0 1 toggle the oc1x output line. 1 0 clear the oc1x output line (to zero). 1 1 set the oc1x output line (to one).
atmega161(l) 47  bit 3 - foc1a: force output compare1a writing a logical one to this bit, forces a change in the compare match output pin pd5 according to the values already set in com1a1 and com1a0. if the com1a1 and com1a0 bits are written in the same cycle as foc1a, the new settings will not take effect until next compare match or forced compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in com1a1 and com1a0 happens as if a compare match had occurred, but no interrupt is generated and it will not clear the timer even if ctc1 in tccr1b is set. the foc1a bit will always be read as zero. the setting of the foc1a bit has no effect in pwm mode.  bit 2 - foc1b: force output compare1b writing a logical one to this bit, forces a change in the compare match output pin pe2 according to the values already set in com1b1 and com1b0. if the com1b1 and com1b0 bits are written in the same cycle as foc1b, the new settings will not take effect until next compare match or forced compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in com1b1 and com1b0 happens as if a compare match had occurred, but no interrupt is generated. the foc1b bit will always be read as zero. the setting of the foc1b bit has no effect in pwm mode.  bits 1..0 - pwm11, pwm10: pulse width modulator select bits these bits select pwm operation of timer/counter1 as specified in table 15. this mode is described on page 50. timer/counter1 control register b ? tccr1b  bit 7 - icnc1: input capture1 noise canceler (4 cks) when the icnc1 bit is cleared (zero), the input capture trigger noise canceler function is disabled. the input capture is trig- gered at the first rising/falling edge sampled on the icp ? input capture pin ? as specified. when the icnc1 bit is set (one), four successive samples are measures on the icp ? input capture pin, and all samples must be high/low according to the input capture trigger specification in the ices1 bit. the actual sampling frequency is xtal clock frequency.  bit 6 - ices1: input capture1 edge select while the ices1 bit is cleared (zero), the timer/counter1 contents are transferred to the input capture register ? icr1 ? on the falling edge of the input capture pin ? icp. while the ices1 bit is set (one), the timer/counter1 contents are trans- ferred to the input capture register ? icr1 ? on the rising edge of the input capture pin ? icp.  bits 5, 4 - res: reserved bits these bits are reserved bits in the atmega161 and always read zero.  bit 3 - ctc1: clear timer/counter1 on compare match when the ctc1 control bit is set (one), the timer/counter1 is reset to $0000 in the clock cycle after a comparea match. if the ctc1 control bit is cleared, timer/counter1 continues counting and is unaffected by a compare match. when a pres- caling of 1 is used, and the comparea register is set to c, the timer will count as follows if ctc1 is set: ... | c-1 | c | 0 | 1 | ... when the prescaler is set to divide by 8, the timer will count like this: ... | c-1, c-1, c-1, c-1, c-1, c-1, c-1, c-1 | c, c, c, c, c, c, c, c | 0, 0, 0, 0, 0, 0, 0, 0 | ... table 15. pwm mode select pwm11 pwm10 description 0 0 pwm operation of timer/counter1 is disabled 0 1 timer/counter1 is an 8-bit pwm 1 0 timer/counter1 is a 9-bit pwm 1 1 timer/counter1 is a 10-bit pwm bit 7 6 5 4 3 2 1 0 $2e ($4e) icnc1 ices1 - - ctc1 cs12 cs11 cs10 tccr1b read/write r/w r/w r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 48 in pwm mode, this bit has a different function. if the ctc1 bit is cleared in pwm mode, the timer/counter1 acts as an up/down counter. if the ctc1 bit is set (one), the timer/counter wraps when it reaches the top value. refer to page 50 for a detailed description.  bits 2,1,0 - cs12, cs11, cs10: clock select1, bit 2,1 and 0 the clock select1 bits 2,1 and 0 define the prescaling source of timer/counter1. the stop condition provides a timer enable/disable function. the ck down divided modes are scaled directly from the ck oscillator clock. if the external pin modes are used for timer/counter1, transitions on pb1/(t1) will clock the counter even if the pin is configured as an output. this feature can give the user sw control of the counting. timer/counter1 register ? tcnt1h and tcnt1l this 16-bit register contains the prescaled value of the 16-bit timer/counter1. to ensure that both the high and low bytes are read and written simultaneously when the cpu accesses these registers, the access is performed using an 8-bit tem- porary register (temp). this temporary register is also used when accessing ocr1a, ocr1b and icr1. if the main program and also interrupt routines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routines.  tcnt1 timer/counter1 write: when the cpu writes to the high byte tcnt1h, the written data is placed in the temp register. next, when the cpu writes the low byte tcnt1l, this byte of data is combined with the byte data in the temp register, and all 16 bits are written to the tcnt1 timer/counter1 register simultaneously. consequently, the high byte tcnt1h must be accessed first for a full 16-bit register write operation.  tcnt1 timer/counter1 read: when the cpu reads the low byte tcnt1l, the data of the low byte tcnt1l is sent to the cpu and the data of the high byte tcnt1h is placed in the temp register. when the cpu reads the data in the high byte tcnt1h, the cpu receives the data in the temp register. consequently, the low byte tcnt1l must be accessed first for a full 16-bit register read operation. the timer/counter1 is realized as an up or up/down (in pwm mode) counter with read and write access. if timer/counter1 is written to and a clock source is selected, the timer/counter1 continues counting in the timer clock cycle after it is preset with the written value. table 16. clock 1 prescale select cs12 cs11 cs10 description 0 0 0 stop, the timer/counter1 is stopped. 001ck 010ck/8 011ck/64 100ck/256 101ck/1024 1 1 0 external pin t1, falling edge 1 1 1 external pin t1, rising edge bit 151413121110 9 8 $2d ($4d) msb tcnt1h $2c ($4c) lsb tcnt1l 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000
atmega161(l) 49 timer/counter1 output compare register ? ocr1ah and ocr1al timer/counter1 output compare register ? ocr1bh and ocr1bl the output compare registers are 16-bit read/write registers. the timer/counter1 output compare registers contain the data to be continuously compared with timer/counter1. actions on compare matches are specified in the timer/counter1 control and status register. a compare match does only occur if timer/counter1 counts to the ocr value. a software write that sets tcnt1 and ocr1a or ocr1b to the same value does not generate a compare match. a compare match will set the compare interrupt flag in the cpu clock cycle following the compare event. since the output compare registers ? ocr1a and ocr1b ? are 16-bit registers, a temporary register temp is used when ocr1a/b are written to ensure that both bytes are updated simultaneously. when the cpu writes the high byte, ocr1ah or ocr1bh, the data is temporarily stored in the temp register. when the cpu writes the low byte, ocr1al or ocr1bl, the temp register is simultaneously written to ocr1ah or ocr1bh. consequently, the high byte ocr1ah or ocr1bh must be written first for a full 16-bit register write operation. the temp register is also used when accessing tcnt1, and icr1. if the main program and also interrupt routines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routines. timer/counter1 input capture register ? icr1h and icr1l the input capture register is a 16-bit read-only register. when the rising or falling edge (according to the input capture edge setting ? ices1) of the signal at the input capture pin ? icp ? is detected, the current value of the timer/counter1 register ? tcnt1 is transferred to the input capture register ? icr1. in the same cycle, the input capture flag ? icf1 ? is set (one). since the input capture register ? icr1 ? is a 16-bit register, a temporary register temp is used when icr1 is read to ensure that both bytes are read simultaneously. when the cpu reads the low byte icr1l, the data is sent to the cpu and the data of the high byte icr1h is placed in the temp register. when the cpu reads the data in the high byte icr1h, the cpu receives the data in the temp register. consequently, the low byte icr1l must be accessed first for a full 16-bit reg- ister read operation. bit 151413121110 9 8 $2b ($4b) msb ocr1ah $2a ($4a) lsb ocr1al 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000 bit 151413121110 9 8 $29 ($49) msb ocr1bh $28 ($48) lsb ocr1bl 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000 bit 151413121110 9 8 $25 ($45) msb icr1h $24 ($44) lsb icr1l 76543210 read/write rrrrrrrr rrrrrrrr initial value 0 0 0 0 0 0 0 0 00000000
atmega161(l) 50 the temp register is also used when accessing tcnt1, ocr1a and ocr1b. if the main program and also interrupt rou- tines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routine. timer/counter1 in pwm mode when the pwm mode is selected, timer/counter1 and the output compare register1a ? ocr1a and the output com- pare register1b ? ocr1b, form a dual 8, 9 or 10-bit, free-running, glitch-free and phase correct pwm with outputs on the pd5(oc1a) and pe2(oc1b) pins. in this mode the timer/counter1 acts as an up/down counter, counting up from $0000 to top (see table 17), where it turns and counts down again to zero before the cycle is repeated. when the counter value matches the contents of the 8,9 or 10 least significant bits (depends of the resolution) of ocr1a or ocr1b, the pd5(oc1a)/pe2(oc1b) pins are set or cleared according to the settings of the com1a1/com1a0 or com1b1/com1b0 bits in the timer/counter1 control register tccr1a. refer to table 18 for details. alternatively, the timer/counter1 can be configured to a pwm that operates at twice the speed as in the mode described above. then the timer/counter1 and the output compare register1a ? ocr1a and the output compare register1b ? ocr1b, form a dual 8, 9 or 10-bit, free-running and glitch-free pwm with outputs on the pd5(oc1a) and pe2(oc1b) pins. note: x = a or b as shown in table 17, the pwm operates at either 8-, 9- or 10 bits resolution. note the unused bits in ocr1a, ocr1b and tcnt1 will automatically be written to zero by hardware. i.e. bit 9 to 15 will be set to zero in ocr1a, ocr1b and tcnt1 if the 9-bit pwm resolution is selected. this makes it possible for the user to perform read-modify-write operations in any of the three resolution modes and the unused bits will be treated as don ? t care. note: x = a or b table 17. timer top values and pwm frequency ctc1 pwm11 pwm10 pwm resolution timer top value frequency 0 0 1 8-bit $00ff (255) f tck1 /510 0 1 0 9-bit $01ff (511) f tck1 /1022 0 1 1 10-bit $03ff(1023) f tck1 /2046 1 0 1 8-bit $00ff (255) f tck1 /256 1 1 0 9-bit $01ff (511) f tck1 /512 1 1 1 10-bit $03ff(1023) f tck1 /1024 table 18. compare1 mode select in pwm mode ctc1 com1x1 com1x0 effect on ocx1 0 0 0 not connected 0 0 1 not connected 01 0 cleared on compare match, up-counting. set on compare match, down-counting (non-inverted pwm). 01 1 cleared on compare match, down-counting. set on compare match, up-counting (inverted pwm). 1 0 0 not connected 1 0 1 not connected 1 1 0 cleared on compare match, set on overflow. 1 1 1 set on compare match, cleared on overflow.
atmega161(l) 51 note that in the pwm mode, the 8, 9 or 10 least significant ocr1a/ocr1b bits (depends of resolution), when written, are transferred to a temporary location. they are latched when timer/counter1 reaches the value top. this prevents the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr1a/ocr1b write. see figure 38 and figure 39 for an example in each mode. figure 38. effects on unsynchronized ocr1 latching figure 39. effects of unsynchronized ocr1 latching in overflow mode. during the time between the write and the latch operation, a read from ocr1a or ocr1b will read the contents of the tem- porary location. this means that the most recently written value always will read out of ocr1a/b. when the ocr1x contains $0000 or top, and the up/down pwm mode is selected, the output oc1a/oc1b is updated to low or high on the next compare match according to the settings of com1a1/com1a0 or com1b1/com1b0. this is shown in table 19. in overflow pwm mode, the output oc1a/oc1b is held low or high only when the output compare register contains top. counter v alue compare v alue pwm output oc1x synchronized ocr1x latch counter value compare value pwm output oc1x unsynchronized ocr1x latch glitch compare value changes note: x = a or b compare v alue changes pwm output oc1x pwm output oc1x unsynchronized oc1x latch synchronized oc1x latch note: x = a or b
atmega161(l) 52 note: x = a or b in overflow pwm mode, the table above is only valid for ocr1x = top. in up/down pwm mode, the timer overflow flag1, tov1, is set when the counter advances from $0000. in overflow pwm mode, the timer overflow flag is set as in normal timer/counter mode. timer overflow interrupt1 operates exactly as in normal timer/counter mode, i.e. it is executed when tov1 is set provided that timer overflow interrupt1 and global inter- rupts are enabled. this does also apply to the timer output compare1 flags and interrupts. watchdog timer the watchdog timer is clocked from a separate on-chip oscillator which runs at 1mhz. this is the typical value at v cc = 5v. see characterization data for typical values at other v cc levels. by controlling the watchdog timer prescaler, the watchdog reset interval can be adjusted, see table 20 on page 53 for a detailed description. the wdr ? watchdog reset ? instruction resets the watchdog timer. eight different clock cycle periods can be selected to determine the reset period. if the reset period expires without another watchdog reset, the atmega161 resets and executes from the reset vector. for timing details on the watchdog reset, refer to page 27. to prevent unintentional disabling of the watchdog, a special turn-off sequence must be followed when the watchdog is dis- abled.refer to the description of the watchdog timer control register for details. figure 40. watchdog timer table 19. pwm outputs ocr1x = $0000 or top com1x1 com1x0 ocr1x output oc1x 1 0 $0000 l 10top h 1 1 $0000 h 11top l
atmega161(l) 53 watchdog timer control register ? wdtcr  bits 7..5 - res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero.  bit 4 - wdtoe: watch dog turn-off enable this bit must be set (one) when the wde bit is cleared. otherwise, the watchdog will not be disabled. once set, hardware will clear this bit to zero after four clock cycles. refer to the description of the wde bit for a watchdog disable procedure.  bit 3 - wde: watch dog enable when the wde is set (one) the watchdog timer is enabled, and if the wde is cleared (zero) the watchdog timer function is disabled. wde can only be cleared if the wdtoe bit is set(one). to disable an enabled watchdog timer, the following procedure must be followed: 1. in the same operation, write a logical one to wdtoe and wde. a logical one must be written to wde even though it is set to one before the disable operation starts. 2. within the next four clock cycles, write a logical 0 to wde. this disables the watchdog.  bits 2..0 - wdp2, wdp1, wdp0: watch dog timer prescaler 2, 1 and 0 the wdp2, wdp1 and wdp0 bits determine the watchdog timer prescaling when the watchdog timer is enabled. the different prescaling values and their corresponding time-out periods are shown in table 20. note: the frequency of the watchdog oscillator is voltage dependent as shown in the electrical characteristics section. the wdr ? watchdog reset ? instruction should always be executed before the watchdog timer is enabled. this ensures that the reset period will be in accordance with the watchdog timer prescale settings. if the watchdog timer is enabled without reset, the watchdog timer may not start counting from zero. bit 76543210 $21 ($41) - - - wdtoe wde wdp2 wdp1 wdp0 wdtcr read/write r r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 20. watch dog timer prescale select wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at vcc = 3.0v typical time-out at vcc = 5.0v 0 0 0 16k cycles 47 ms 15 ms 0 0 1 32k cycles 94 ms 30 ms 0 1 0 64k cycles 0.19 s 60 ms 0 1 1 128k cycles 0.38 s 0.12 s 1 0 0 256k cycles 0.75 s 0,24 s 1 0 1 512k cycles 1.5 s 0.49 s 1 1 0 1,024k cycles 3.0 s 0.97 s 1 1 1 2,048k cycles 6.0 s 1.9 s
atmega161(l) 54 eeprom read/write access the eeprom access registers are accessible in the i/o space. the write access time is in the range of 2.5 - 4 ms, depending on the v cc voltages. a self-timing function, however, lets the user software detect when the next byte can be written. if the user code contains code that writes the eeprom, some pre- caution must be taken. in heavily filtered power supplies, v cc is likely to rise or fall slowly on power-up/down. this causes the device for some period of time to run at a voltage lower than specified as minimum for the clock frequency used. cpu operation under these conditions is likely cause the program counter to perform unintentional jumps and eventually execute the eeprom write code. to secure eeprom integrity, the user is advised to use an external under-voltage reset circuit in this case. in order to prevent unintentional eeprom writes, a specific write procedure must be followed. refer to the description of the eeprom control register for details on this. when the eeprom is read or written, the cpu is halted for two clock cycles before the next instruction is executed. eeprom address register ? eearh and eearl  bits 15..9 - res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero.  bits 8..0 - eear8..0: eeprom address the eeprom address registers ? eearh and eearl specify the eeprom address in the 512 bytes eeprom space. the eeprom data bytes are addressed linearly between 0 and 511. the initial value of eear is undefined. a proper value must be written before the eeprom may be accessed. eeprom data register ? eedr  bits 7..0 - eedr7..0: eeprom data for the eeprom write operation, the eedr register contains the data to be written to the eeprom in the address given by the eear register. for the eeprom read operation, the eedr contains the data read out from the eeprom at the address given by eear. eeprom control register ? eecr  bit 7..4 - res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero. bit 151413121110 9 8 $1f ($3f) - - - - - - - eear8 eearh $1e ($3e) eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 eearl 76543210 read/write rrrrrrrr/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 x xxxxxxxx bit 76543210 $1d ($3d) msb lsb eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $1c ($3c) - - - - eerie eemwe eewe eere eecr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 55  bit 3 - eerie: eeprom ready interrupt enable when the i bit in sreg and eerie are set (one), the eeprom ready interrupt is enabled. when cleared (zero), the inter- rupt is disabled. the eeprom ready interrupt generates a constant interrupt when eewe is cleared (zero).  bit 2 - eemwe: eeprom master write enable the eemwe bit determines whether setting eewe to one causes the eeprom to be written. when eemwe is set(one) setting eewe will write data to the eeprom at the selected address. if eemwe is zero, setting eewe will have no effect. when eemwe has been set (one) by software, hardware clears the bit to zero after four clock cycles. see the description of the eewe bit for an eeprom write procedure.  bit 1 - eewe: eeprom write enable the eeprom write enable signal eewe is the write strobe to the eeprom. when address and data are correctly set up, the eewe bit must be set to write the value into the eeprom. the eemwe bit must be set when the logical one is written to eewe, otherwise no eeprom write takes place. the following procedure should be followed when writing the eeprom (the order of steps 2 and 3 is not essential): 1. wait until eewe becomes zero. 2. write new eeprom address to eear (optional). 3. write new eeprom data to eedr (optional). 4. write a logical one to the eemwe bit in eecr. 5. within four clock cycles after setting eemwe, write a logical one to eewe. caution: an interrupt between step 4 and step 5 will make the write cycle fail, since the eeprom master write enable will time-out. if an interrupt routine accessing the eeprom is interrupting another eeprom access, the eear or eedr regis- ter will be modified, causing the interrupted eeprom access to fail. it is recommended to have the global interrupt flag cleared during the 4 last steps to avoid these problems. when the write access time (typically 2.5 ms at v cc = 5v or 4 ms at v cc = 2.7v) has elapsed, the eewe bit is cleared (zero) by hardware. the user software can poll this bit and wait for a zero before writing the next byte. when eewe has been set, the cpu is halted for two cycles before the next instruction is executed.  bit 0 - eere: eeprom read enable the eeprom read enable signal eere is the read strobe to the eeprom. when the correct address is set up in the eear register, the eere bit must be set. when the eere bit is cleared (zero) by hardware, requested data is found in the eedr register. the eeprom read access takes one instruction and there is no need to poll the eere bit. when eere has been set, the cpu is halted for four cycles before the next instruction is executed. the user should poll the eewe bit before starting the read operation. if a write operation is in progress when new data or address is written to the eeprom i/o registers, the write operation will be interrupted, and the result is undefined. prevent eeprom corruption during periods of low v cc, the eeprom data can be corrupted because the supply voltage is too low for the cpu and the eeprom to operate properly. these issues are the same as for board level systems using the eeprom, and the same design solutions should be applied. an eeprom data corruption can be caused by two situations when the voltage is too low. first, a regular write sequence to the eeprom requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instructions incor- rectly, if the supply voltage for executing instructions is too low. eeprom data corruption can easily be avoided by following these design recommendations (one is sufficient): 1. keep the avr reset active (low) during periods of insufficient power supply voltage. this can be done by enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an external low v cc reset protection circuit can be applied. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will prevent the cpu from attempt- ing to decode and execute instructions, effectively protecting the eeprom registers from unintentional writes. 3. store constants in flash memory if the ability to change memory contents from software is not required. flash memory can not be updated by the cpu, and will not be subject to corruption.
atmega161(l) 56 serial peripheral interface ? spi the serial peripheral interface (spi) allows high-speed synchronous data transfer between the atmega161 and peripheral devices or between several avr devices. the atmega161 spi features include the following:  full-duplex, 3-wire synchronous data transfer  master or slave operation  lsb first or msb first data transfer  seven programmable bit rates  end of transmission interrupt flag  write collision flag protection  wake-up from idle mode (slave mode only)  double speed (ck/2) master spi mode figure 41. spi block diagram spi2x spi2x divider /2/4/8/16/32/64/128
atmega161(l) 57 the interconnection between master and slave cpus with spi is shown in figure 42. the pb7(sck) pin is the clock output in the master mode and is the clock input in the slave mode. writing to the spi data register of the master cpu starts the spi clock generator, and the data written shifts out of the pb5(mosi) pin and into the pb5(mosi) pin of the slave cpu. after shifting one byte, the spi clock generator stops, setting the end of transmission flag (spif). if the spi interrupt enabl e bit (spie) in the spcr register is set, an interrupt is requested. the slave select input, pb4(ss ), is set low to select an individual slave spi device. the two shift registers in the master and the slave can be considered as one distributed 16-bit circular shift register. this is shown in figure 42. when data is shifted from the master to the slave, data is also shifted in the opposite direction, simultaneously. this means that during one shift cycle, data in the master and the slave are interchanged. figure 42. spi master-slave interconnection the system is single buffered in the transmit direction and double buffered in the receive direction. this means that bytes to be transmitted cannot be written to the spi data register before the entire shift cycle is completed. when receiving data, however, a received byte must be read from the spi data register before the next byte has been completely shifted in. otherwise, the first byte is lost. when the spi is enabled, the data direction of the mosi, miso, sck and ss pins is overridden according to the following table: note: see ? alternate functions of port b ? on page 80 f or a detailed description of how to define the direction of the user defined spi pins. table 21. spi pin overrides pin direction, master spi direction, slave spi mosi user defined input miso input user defined sck user defined input ss user defined input
atmega161(l) 58 ss pin functionality when the spi is configured as a master (mstr in spcr is set), the user can determine the direction of the ss pin. if ss is configured as an output, the pin is a general output pin which does not affect the spi system. if ss is configured as an input, it must be hold high to ensure master spi operation. if the ss pin is driven low by peripheral circuitry when the spi is configured as master with the ss pin defined as an input, the spi system interprets this as another master selecting the spi as a slave and starting to send data to it. to avoid bus contention, the spi system takes the following actions: 1. the mstr bit in spcr is cleared and the spi system becomes a slave. as a result of the spi becoming a slave, the mosi and sck pins become inputs. 2. the spif flag in spsr is set, and if the spi interrupt is enabled and the i-bit in sreg is set, the interrupt routine will be executed. thus, when interrupt-driven spi transmission is used in master mode, and there exists a possibility that ss is driven low, the interrupt should always check that the mstr bit is still set. once the mstr bit has been cleared by a slave select, it must be set by the user to re-enable spi master mode. when the spi is configured as a slave, the ss pin is always input. when ss is held low, the spi is activated and miso becomes an output if configured so by the user. all other pins are inputs. when ss is driven high, all pins are inputs, and the spi is passive, which means that it will not receive incoming data. note that the spi logic will be reset once the ss pin is brought high. if the ss pin is brought high during a transmission, the spi will stop sending and receiving immediately and both data received and data sent must be considered as lost. data modes there are four combinations of sck phase and polarity with respect to serial data, which are determined by control bits cpha and cpol. the spi data transfer formats are shown in figure 43 and figure 44. figure 43. spi transfer format with cpha = 0 and dord = 0 figure 44. spi transfer format with cpha = 1 and dord = 0
atmega161(l) 59 spi control register ? spcr  bit 7 - spie: spi interrupt enable this bit causes the spi interrupt to be executed if spif bit in the spsr register is set and the if the global interrupt enable bit in sreg is set.  bit 6 - spe: spi enable when the spe bit is set (one), the spi is enabled. this bit must be set to enable any spi operations.  bit 5 - dord: data order when the dord bit is set (one), the lsb of the data word is transmitted first. when the dord bit is cleared (zero), the msb of the data word is transmitted first.  bit 4 - mstr: master/slave select this bit selects master spi mode when set (one), and slave spi mode when cleared (zero). if ss is configured as an input and is driven low while mstr is set, mstr will be cleared, and spif in spsr will become set. the user will then have to set mstr to re-enable spi master mode.  bit 3 - cpol: clock polarity when this bit is set (one), sck is high when idle. when cpol is cleared (zero), sck is low when idle. refer to figure 43 and figure 44 for additional information.  bit 2 - cpha: clock phase refer to figure 43 or figure 44 for the functionality of this bit.  bits 1,0 - spr1, spr0: spi clock rate select 1 and 0 these two bits control the sck rate of the device configured as a master. spr1 and spr0 have no effect on the slave. the relationship between sck and the oscillator clock frequency f cl is shown in table 22: note: when the spi is configured as slave, the spi is only guaranteed to work at f cl/ 4. spi status register ? spsr bit 76543210 $0d ($2d) spie spe dord mstr cpol cpha spr1 spr0 spcr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 22. relationship between sck and the oscillator frequency spi2x spr1 spr0 sck frequency 00 0 f cl/ 4 00 1 f cl / 16 01 0 f cl / 64 01 1 f cl/ 128 10 0 f cl/ 2 10 1 f cl / 8 11 0 f cl / 32 11 1 f cl/ 64 bit 76543210 $0e ($2e) spif wcol - - - - - spi2x spsr read/write rrrrrrrr/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 60  bit 7 - spif: spi interrupt flag when a serial transfer is complete, the spif bit is set (one) and an interrupt is generated if spie in spcr is set (one) and global interrupts are enabled. if ss is an input and is driven low when the spi is in master mode, this will also set the spif flag. spif is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, the spif bit is cleared by first reading the spi status register with spif set (one), then accessing the spi data register (spdr).  bit 6 - wcol: write collision flag the wcol bit is set if the spi data register (spdr) is written during a data transfer. the wcol bit (and the spif bit) are cleared (zero) by first reading the spi status register with wcol set (one), and then accessing the spi data register.  bit 5..1 - res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero.  bit 0 - spi2x: double spi speed bit when this bit is set (one) the spi speed (sck frequency) will be doubled when the spi is in master mode (see table 22). this means that the maximum sck period will be 2 cpu clock periods. when the spi is configured as slave, the spi is only guaranteed to work at fcl/4. the spi interface on the atmega161 is also used for program memory and eeprom downloading or uploading. see page 109 for serial programming and verification. spi data register ? spdr the spi data register is a read/write register used for data transfer between the register file and the spi shift register. writing to the register initiates data transmission. reading the register causes the shift register receive buffer to be read. uarts the atmega161 features two full duplex (separate receive and transmit registers) universal asynchronous receiver and transmitter (uart). the main features are:  baud rate generator generates any baud rate  high baud rates at low xtal frequencies  8 or 9 bits data  noise filtering  overrun detection  framing error detection  false start bit detection  three separate interrupts on tx complete, tx data register empty and rx complete  multi-processor communication mode  double speed uart mode data transmission a block schematic of the uart transmitter is shown in figure 45. the two uarts are identical and the functionality is described in general for the two uarts. bit 76543210 $0f ($2f) msb lsb spdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value xxxxxxxxundefined
atmega161(l) 61 figure 45. uart transmitter data transmission is initiated by writing the data to be transmitted to the uart i/o data register, udrn. data is trans- ferred from udrn to the transmit shift register when:  a new character has been written to udrn after the stop bit from the previous character has been shifted out. the shift register is loaded immediately.  a new character has been written to udrn before the stop bit from the previous character has been shifted out. the shift register is loaded when the stop bit of the character currently being transmitted has been shifted out. if the 10(11)-bit transmitter shift register is empty, data is transferred from udrn to the shift register. at this time the udren (uart data register empty) bit in the uart control and status register, ucsrna, is set. when this bit is set (one), the uart is ready to receive the next character. at the same time as the data is transferred from udrn to the 10(11)-bit shift register, bit 0 of the shift register is cleared (start bit) and bit 9 or 10 is set (stop bit). if 9 bit data word is selected (the chr9n bit in the uart control and status register, ucsrnb is set), the txb8 bit in ucsrnb is transferred to bit 9 in the transmit shift register. on the baud rate clock following the transfer operation to the shift register, the start bit is shifted out on the txdn pin. then follows the data, lsb first. when the stop bit has been shifted out, the shift register is loaded if any new data has been written to the udrn during the transmission. during loading, udren is set. if there is no new data in the udrn regis- ter to send when the stop bit is shifted out, the udren flag will remain set until udrn is written again. when no new data has been written, and the stop bit has been present on txdn for one bit length, the tx complete flag, txcn, in ucsrna is set. pd1/ pb3 u2xn 10(11)-bit tx shift register pin control logic uart control and status register (ucsrna) uart control and status register (ucsrnb) control logic baud rate generator txcn irq udren irq data bus rxcien txcien udrien txcn udren rxenn txenn chr9n rxb8n txb8n rxcn txcn udren fen mpcmpn orn idle baud store udrn shift enable data b u s baud x 16 /16 uart i/o data register (udrn) xtal txdn n = 0,1
atmega161(l) 62 the txenn bit in ucsrnb enables the uart transmitter when set (one). when this bit is cleared (zero), the pd1 (uart0) or pb3 (uart1) pin can be used for general i/o. when txenn is set, the uart transmitter will be connected to pd1 (uart0) or pb3 (uart1), which is forced to be an output pin regardless of the setting of the ddd1 bit in ddrd (uart0) or ddb3 in ddrb (uart1). note that pb3 (uart1) also is used as one of the input pins to the analog comparator. it is therefore not recommended to use uart1 if the analog comparator also is used in the application at the same time. data reception figure 46 shows a block diagram of the uart receiver figure 46. uart receiver the receiver front-end logic samples the signal on the rxdn pin at a frequency 16 times the baud rate. while the line is idle, one single sample of logical zero will be interpreted as the falling edge of a start bit, and the start bit detection sequence is initiated. let sample 1 denote the first zero-sample. following the 1 to 0-transition, the receiver samples the rxdn pin at samples 8, 9 and 10. if two or more of these three samples are found to be logical ones, the start bit is rejected as a noise spike and the receiver starts looking for the next 1 to 0-transition. if however, a valid start bit is detected, sampling of the data bits following the start bit is performed. these bits are also sampled at samples 8, 9 and 10. the logical value found in at least two of the three samples is taken as the bit value. all bits are shifted into the transmitter shift register as they are sampled. sampling of an incoming character is shown in figure 47. note that the description above is not valid when the uart transmission speed is doubled. see ? double speed trans- mission ? on page 68 for a detailed description. pd0/ pb2 data b u s baud uart i/o data register (udrn) 10(11)-bit rx shift register store udrn /16 baud x 16 rxenn txenn chr9n rxb8n txb8n u2xn rxcn txcn udren fen mpcmpn orn uart control and status register (ucsrnb) uart control and status register (ucsrna) rxcien txcien udrien txcn data b u s pin control logic baud rate generator xtal n = 0,1 rxcn irq data recovery logic rxdn
atmega161(l) 63 figure 47. sampling received data note: this figure is not valid when the uart speed is doubled. see ? double speed transmission ? on page 68 for a detailed description. when the stop bit enters the receiver, the majority of the three samples must be one to accept the stop bit. if two or more samples are logical zeros, the framing error (fen) flag in the uart control and status register (ucsrna) is set. before reading the udrn register, the user should always check the fen bit to detect framing errors. whether or not a valid stop bit is detected at the end of a character reception cycle, the data is transferred to udrn and the rxcn flag in ucsrna is set. udrn is in fact two physically separate registers, one for transmitted data and one for received data. when udrn is read, the receive data register is accessed, and when udrn is written, the transmit data register is accessed. if 9 bit data word is selected (the chr9n bit in the uart control and status register, ucsrnb is set), the rxb8n bit in ucsrnb is loaded with bit 9 in the transmit shift register when data is transferred to udrn. if, after having received a character, the udrn register has not been read since the last receive, the overrun (orn) flag in ucsrnb is set. this means that the last data byte shifted into to the shift register could not be transferred to udrn and has been lost. the orn bit is buffered, and is updated when the valid data byte in udrn is read. thus, the user should always check the orn bit after reading the udrn register in order to detect any overruns if the baud rate is high or cpu load is high. when the rxen bit in the ucsrnb register is cleared (zero), the receiver is disabled. this means that the pd0 pin can be used as a general i/o pin. when rxen is set, the uart receiver will be connected to pd0 (uart0) or pb2 (uart1), which is forced to be an input pin regardless of the setting of the ddd0 in ddrd (uart0) or ddb2 bit in ddrb (uart1). when pd0 (uart0) or pb2 (uart1) is forced to input by the uart, the portd0 (uart0) or portb2 (uart1) bit can still be used to control the pull-up resistor on the pin. note that pb2 (uart1) also is used as one of the input pins to the analog comparator. it is therefor not recommended to use uart1 if the analog comparator also is used in the application at the same time. when the chr9n bit in the ucsrnb register is set, transmitted and received characters are 9-bit long plus start and stop bits. the 9th data bit to be transmitted is the txb8n bit in ucsrnb register. this bit must be set to the wanted value before a transmission is initiated by writing to the udrn register. the 9th data bit received is the rxb8n bit in the ucsrnb register. multi-processor communication mode the multi-processor communication mode enables several slave mcus to receive data from a master mcu. this is done by first decoding an address byte to find out which mcu has been addressed. if a particular slave mcu has been addressed, it will receive the following data bytes as normal, while the other slave mcus will ignore the data bytes until another address byte is received. for an mcu to act as a master mcu, it should enter 9-bit transmission mode (chr9n in ucsrnb set). the 9th bit must be one to indicate that an address byte is being transmitted, and zero to indicate that a data byte is being transmitted. for the slave mcus, the mechanism appears slightly differently for 8-bit and 9-bit reception mode. in 8-bit reception mode (chr9n in ucsrnb cleared), the stop bit is one for an address byte and zero for a data byte. in 9-bit reception mode (chr9n in ucsrnb set), the 9th bit is one for an address byte and zero for a data byte, whereas the stop bit is always high. the following procedure should be used to exchange data in multi-processor communication mode: 1. all slave mcus are in multi-processor communication mode (mpcmn in ucsrna is set). 2. the master mcu sends an address byte, and all slaves receive and read this byte. in the slave mcus, the rxcn flag in ucsrna will be set as normal. 3. each slave mcu reads the udrn register and determines if it has been selected. if so, it clears the mpcmn bit in ucsrna, otherwise it waits for the next address byte.
atmega161(l) 64 4. for each received data byte, the receiving mcu will set the receive complete flag (rxcn in ucsrna. in 8-bit mode, the receiving mcu will also generate a framing error (fen in ucsrna set), since the stop bit is zero. the other slave mcus, which still have the mpcmn bit set, will ignore the data byte. in this case, the udrn register and the rxcn, fen, or flags will not be affected. 5. after the last byte has been transferred, the process repeats from step 2. uart control uart0 i/o data register ? udr0 uart1 i/o data register ? udr1 the udrn register is actually two physically separate registers sharing the same i/o address. when writing to the register, the uart transmit data register is written. when reading from udrn, the uart receive data register is read. uart0 control and status registers ? ucsr0a uart1 control and status registers ? ucsr1a  bit 7 - rxc0/rxc1: uart receive complete this bit is set (one) when a received character is transferred from the receiver shift register to udrn. the bit is set regard- less of any detected framing errors. when the rxcien bit in ucsrnb is set, the uart receive complete interrupt will be executed when rxcn is set(one). rxcn is cleared by reading udrn. when interrupt-driven data reception is used, the uart receive complete interrupt routine must read udrn in order to clear rxcn, otherwise a new interrupt will occur once the interrupt routine terminates.  bit 6 - txc0/txc1: uart transmit complete this bit is set (one) when the entire character (including the stop bit) in the transmit shift register has been shifted out an d no new data has been written to udrn. this flag is especially useful in half-duplex communications interfaces, where a transmitting application must enter receive mode and free the communications bus immediately after completing the transmission. when the txcien bit in ucsrnb is set, setting of txcn causes the uart transmit complete interrupt to be executed. txcn is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, the txcn bit is cleared (zero) by writing a logical one to the bit. bit 76543210 $0c ($2c) msb lsb udr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $03 ($23) msb lsb udr1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $0b ($2b) rxc0 txc0 udre0 fe0 or0 - u2x0 mpcm0 ucsr0a read/write r r/w r r r r r/w r/w initial value 0 0 1 0 0 0 0 0 bit 76543210 $02 ($22) rxc1 txc1 udre1 fe1 or1 - u2x1 mpcm1 ucsr1a read/write r r/w r r r r r/w r/w initial value 0 0 1 0 0 0 0 0
atmega161(l) 65  bit 5 - udre0/udre1: uart data register empty this bit is set (one) when a character written to udrn is transferred to the transmit shift register. setting of this bit indi- cates that the transmitter is ready to receive a new character for transmission. when the udrien bit in ucsrnb is set, the uart transmit complete interrupt will be executed as long as udren is set and the global interrupt enable bit in sreg is set. udren is cleared by writing udrn. when interrupt-driven data transmit- tal is used, the uart data register empty interrupt routine must write udrn in order to clear udren, otherwise a new interrupt will occur once the interrupt routine terminates. udren is set (one) during reset to indicate that the transmitter is ready.  bit 4 - fe0/fe1: framing error this bit is set if a framing error condition is detected, i.e. when the stop bit of an incoming character is zero. the fen bit is cleared when the stop bit of received data is one.  bit 3 - or0/or1: overrun this bit is set if an overrun condition is detected, i.e. when a character already present in the udrn register is not read before the next character has been shifted into the receiver shift register. the orn bit is buffered, which means that it will be set once the valid data still in udrn is read. the orn bit is cleared (zero) when data is received and transferred to udrn.  bit 2 - res: reserved bit this bit is reserved bit in the atmega161 and will always read as zero.  bits 1 - u2x0/u2x1: double the uart transmission speed when this bit is set (one) the uart speed will be doubled. this means that a bit will be transmitted/received in 8 cpu clock periods instead of 16 cpu clock periods. for a detailed description, see ? double speed transmission ? on page 68 ? .  bit 0 - mpcm0/mpcm1: multi-processor communication mode this bit is used to enter multi-processor communication mode. the bit is set when the slave mcu waits for an address byte to be received. when the mcu has been addressed, the mcu switches off the mpcmn bit, and starts data reception. for a detailed description, see ? multi-processor communication mode ? . uart0 control and status registers ? ucsr0b uart1 control and status registers ? ucsr1b  bit 7 - rxcie0/rxcie1: rx complete interrupt enable when this bit is set (one), a setting of the rxcn bit in ucsrna will cause the receive complete interrupt routine to be executed provided that global interrupts are enabled.  bit 6 - txcie0/txcie1: tx complete interrupt enable when this bit is set (one), a setting of the txcn bit in ucsrna will cause the transmit complete interrupt routine to be executed provided that global interrupts are enabled.  bit 5 - udrie0/udrei1: uart data register empty interrupt enable when this bit is set (one), a setting of the udren bit in ucsrna will cause the uart data register empty interrupt routine to be executed provided that global interrupts are enabled.  bit 4 - rxen0/rxen1: receiver enable this bit enables the uart receiver when set (one). when the receiver is disabled, the txcn, orn and fen status flags cannot become set. if these flags are set, turning off rxen does not cause them to be cleared. bit 76543210 $0a ($2a) rxcie0 txcie0 udrie0 rxen0 txen0 chr90 rxb80 txb80 ucsr0b read/write r/w r/w r/w r/w r/w r/w r r/w initial value 0 0 0 0 0 0 1 0 bit 76543210 $01 ($21) rxcie1 txcie1 udrie1 rxen1 txen1 chr91 rxb81 txb81 ucsr1b read/write r/w r/w r/w r/w r/w r/w r r/w initial value 0 0 0 0 0 0 1 0
atmega161(l) 66  bit 3 - txen0/txen1: transmitter enable this bit enables the uart transmitter when set (one). when disabling the transmitter while transmitting a character, the transmitter is not disabled before the character in the shift register plus any following character in udrn has been com- pletely transmitted.  bit 2 - chr90/chr91: 9 bit characters when this bit is set (one) transmitted and received characters are 9 bit long plus start and stop bits. the 9th bit is read and written by using the rxb8n and txb8 bits in ucsrnb, respectively. the 9th data bit can be used as an extra stop bit or a parity bit.  bit 1 - rxb80/rxb81: receive data bit 8 when chr9n is set (one), rxb8n is the 9th data bit of the received character.  bit 0 - txb80/txb81: transmit data bit 8 when chr9n is set (one), txb8n is the 9th data bit in the character to be transmitted. baud rate generator the baud rate generator is a frequency divider which generates baud-rates according to the following equation:  baud = baud-rate  f ck = crystal clock frequency  ubr = contents of the ubrrh and ubrr registers, (0-4095)  note that this equation is not valid when the uart transmission speed is doubled. see ? double speed transmission ? on page 68 for a detailed description. for standard crystal frequencies, the most commonly used baud rates can be generated by using the ubr settings in table 23. ubr values which yield an actual baud rate differing less than 2% from the target baud rate, are bold in the table. however, using baud rates that have more than 1% error is not recommended. high error ratings give less noise resistance. baud f ck 16(ubr 1 ) + --------------------------------- =
atmega161(l) 67 table 23. ubr settings at various crystal frequencies uart0 and uart1 high byte baud rate register ubrrhi the uart baud register is a 12-bit register. the 4 most significant bits are located in a separate register, ubrrhi. note that both uart0 and uart1 share this register. bit 7 to bit 4 of ubrrhi contain the 4 most significant bits of the uart1 baud register. bit3 to bit0 contain the 4 most significant bits of the uart0 baud register. bit 76543210 $20 ($40) msb1 lsb1 msb0 lsb0 ubrrhi read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 b aud rate 1mhz % e rro r 1.8432 m hz %error 2mhz % e rro r 2400 ubr= 25 0.2 ubr= 47 0.0 ubr= 51 0.2 4800 ubr= 12 0.2 ubr= 23 0.0 ubr= 25 0.2 9600 ubr= 6 7.5 ubr= 11 0.0 ubr= 12 0.2 14400 ubr= 3 7.8 ubr= 70.0 ubr= 8 3.7 19200 ubr= 2 7.8 ubr= 50.0 ubr= 6 7.5 28800 ubr= 1 7.8 ubr= 30.0 ubr= 3 7.8 38400 ubr= 1 22.9 ubr= 20.0 ubr= 2 7.8 57600 ubr= 0 7.8 ubr= 10.0 ubr= 1 7.8 76800 ub r= 0 22.9 ub r= 1 33.3 ub r= 1 22.9 115200 ubr= 0 84.3 ubr= 00.0 ubr= 0 7.8 b aud rate 3.2768 m hz % e rro r 3.6864 m hz %error 4mhz % e rro r 2400 ubr= 84 0.4 ubr= 95 0.0 ubr= 103 0.2 4800 ubr= 42 0.8 ubr= 47 0.0 ubr= 51 0.2 9600 ubr= 20 1.6 ubr= 23 0.0 ubr= 25 0.2 14400 ubr= 13 1.6 ubr= 15 0.0 ubr= 16 2.1 19200 ubr= 10 3.1 ubr= 11 0.0 ubr= 12 0.2 28800 ubr= 61.6 ubr= 70.0 ubr= 8 3.7 38400 ubr= 4 6.3 ubr= 50.0 ubr= 6 7.5 57600 ubr= 3 12.5 ubr= 30.0 ubr= 3 7.8 76800 ubr= 2 12.5 ubr= 20.0 ubr= 2 7.8 115200 ubr= 1 12.5 ubr= 10.0 ubr= 1 7.8 b aud rate 7.3728 m hz % e rro r 8mhz %error 9.216 m hz % e rro r 2400 ubr= 191 0.0 ubr= 207 0.2 ubr= 239 0.0 4800 ubr= 95 0.0 ubr= 103 0.2 ubr= 119 0.0 9600 ubr= 47 0.0 ubr= 51 0.2 ubr= 59 0.0 14400 ubr= 31 0.0 ubr= 34 0.8 ubr= 39 0.0 19200 ubr= 23 0.0 ubr= 25 0.2 ubr= 29 0.0 28800 ubr= 15 0.0 ubr= 16 2.1 ubr= 19 0.0 38400 ubr= 11 0.0 ubr= 12 0.2 ubr= 14 0.0 57600 ubr= 70.0 ubr= 8 3.7 ubr= 90.0 76800 ubr= 50.0 ubr= 6 7.5 ubr= 7 6.7 115200 ubr= 30.0 ubr= 3 7.8 ubr= 40.0
atmega161(l) 68 uart0 baud rate register low byte ? ubrr0 uart1 baud rate register low byte ? ubrr1 ubrrn stores the 8 least significant bits of the uart baud rate register. double speed transmission the atmega161 provides a separate uart mode that allows the user to double the communication speed. by setting the u2x bit in uart control and status register ucsrna, the uart speed will be doubled. the data reception will differ slightly from normal mode. since the speed is doubled, the receiver front-end logic samples the signals on rxdn pin at a frequency 8 times the baud rate. while the line is idle, one single sample of logical zero will be interpreted as the falling edge of a start bit, and the start bit detection sequence is initiated. let sample 1 denote the first zero-sample. following th e 1 to 0-transition, the receiver samples the rxdn pin at samples 4, 5 and 6. if two or more of these three samples are found to be logical ones, the start bit is rejected as a noise spike and the receiver starts looking for the next 1 to 0-transition. if however, a valid start bit is detected, sampling of the data bits following the start bit is performed. these bits are also sampled at samples 4, 5 and 6. the logical value found in at least two of the three samples is taken as the bit value. all bits are shifted into the transmitter shift register as they are sampled. sampling of an incoming character is shown in figure 48. figure 48. sampling received data when the transmission speed is doubled the baud rate generator in double uart speed mode note that the baud-rate equation is different from the equation at page 66 when the uart speed is doubled:  baud = baud-rate  f ck = crystal clock frequency  ubr = contents of the ubrrhi and ubrr registers, (0-4095)  note that this equation is only valid when the uart transmission speed is doubled. for standard crystal frequencies, the most commonly used baud rates can be generated by using the ubr settings in table 23. ubr values which yield an actual baud rate differing less than 1.5% from the target baud rate, are bold in the table. however since the number of samples are reduced and the system clock might have some variance (this applies especially when using resonators), it is recommended that the baud rate error is less than 0.5%. bit 76543210 $09 ($29) msb lsb ubrr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $00 ($20) msb lsb ubrr1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 start bit d0 d1 d2 d3 d4 d5 d6 d7 stop bit rxd receiver sampling baud f ck 8(ubr 1 ) + ------------------------------ =
atmega161(l) 69 table 24. ubr settings at various crystal frequencies in double speed mode baud rate 1.0000 mhz % error 1.8432 mhz % error 2.0000 mhz % error 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 ubr = 51 ubr = 25 ubr = 12 ubr = 8 ubr = 6 ubr = 3 ubr = 2 ubr = 1 ubr = 1 ubr = 0 - 0.2 0.2 0.2 3.7 7.5 7.8 7.8 7.8 22.9 84.3 - ubr = 95 ubr = 47 ubr = 23 ubr = 15 ubr = 11 ubr = 7 ubr = 5 ubr = 3 ubr = 2 ubr = 1 ubr = 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ubr = 103 ubr = 51 ubr = 25 ubr = 16 ubr = 12 ubr = 8 ubr = 6 ubr = 3 ubr = 2 ubr = 1 ubr = 0 0.2 0.2 0.2 2.1 0.2 3.7 7.5 7.8 7.8 7.8 84.3 baud rate 3.2768 mhz % error 3.6864 mhz % error 4.0000 mhz % error 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 460800 912600 ubr = 170 ubr = 84 ubr = 42 ubr = 27 ubr = 20 ubr = 13 ubr = 10 ubr = 6 ubr = 4 ubr = 3 ubr = 1 ubr = 0 - 0.2 0.4 0.8 1.6 1.6 1.6 3.1 1.6 6.2 12.5 12.5 12.5 - ubr = 191 ubr = 95 ubr = 47 ubr = 31 ubr = 23 ubr = 15 ubr = 11 ubr = 7 ubr = 5 ubr = 3 ubr = 1 ubr = 0 - 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 - ubr = 207 ubr = 103 ubr = 51 ubr = 34 ubr = 25 ubr = 16 ubr = 12 ubr = 8 ubr = 6 ubr = 3 ubr = 1 ubr = 0 ubr = 0 0.2 0.2 0.2 0.8 0.2 2.1 0.2 3.7 7.5 7.8 7.8 7.8 84.3 baud rate 7.3728 mhz % error 8.0000 mhz % error 9.2160 mhz % error 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 460800 912600 ubr = 383 ubr = 191 ubr = 95 ubr = 63 ubr = 47 ubr = 31 ubr = 23 ubr = 15 ubr = 11 ubr = 7 ubr = 3 ubr = 1 ubr = 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ubr = 416 ubr = 207 ubr = 103 ubr = 68 ubr = 51 ubr = 34 ubr = 25 ubr = 16 ubr = 12 ubr = 8 ubr = 3 ubr = 1 ubr = 0 0.1 0.2 0.2 0.6 0.2 0.8 0.2 2.1 0.2 3.7 7.8 7.8 7.8 ubr = 479 ubr = 239 ubr = 119 ubr = 79 ubr = 59 ubr = 39 ubr = 29 ubr = 19 ubr = 14 ubr = 9 ubr = 4 ubr = 2 ubr = 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 20.0 20.0
atmega161(l) 70 analog comparator the analog comparator compares the input values on the positive input pb2 (ain0) and negative input pb3 (ain1). when the voltage on the positive input pb2 (ain0) is higher than the voltage on the negative input pb3 (ain1), the analog com- parator output, aco is set (one). the comparator ? s output can be set to trigger the timer/counter1 input capture function. in addition, the comparator can trigger a separate interrupt, exclusive to the analog comparator. the user can select inter- rupt triggering on comparator output rise, fall or toggle. a block diagram of the comparator and its surrounding logic is shown in figure 49 . figure 49. analog comparator block diagram analog comparator control and status register ? acsr  bit 7 - acd: analog comparator disable when this bit is set(one), the power to the analog comparator is switched off. this bit can be set at any time to turn off the analog comparator. this will reduce power consumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be disabled by clearing the acie bit in acsr. otherwise an interrupt can occur when the bit is changed.  bit 6 - ainbg: analog comparator bandgap select when this bit is set, a fixed bandgap voltage of 1.22 ?0.05v replaces the normal input to the positive input (ain0) of the comparator. when this bit is cleared, the normal input pin pb2 is applied to the positive input of the comparator.  bit 5 - aco: analog comparator output aco is directly connected to the comparator output.  bit 4 - aci: analog comparator interrupt flag this bit is set (one) when a comparator output event triggers the interrupt mode defined by aci1 and aci0. the analog comparator interrupt routine is executed if the acie bit is set (one) and the i-bit in sreg is set (one). aci is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, aci is cleared by writing a logic one to the flag. bit 76543210 $08 ($28) acd ainbg aco aci acie acic acis1 acis0 acsr read/write r/w r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 71  bit 3 - acie: analog comparator interrupt enable when the acie bit is set (one) and the i-bit in the status register is set (one), the analog comparator interrupt is enabled. when cleared (zero), the interrupt is disabled.  bit 2 - acic: analog comparator input capture enable when set (one), this bit enables the input capture function in timer/counter1 to be triggered by the analog comparator. the comparator output is in this case directly connected to the input capture front-end logic, making the comparator utilize the noise canceler and edge select features of the timer/counter1 input capture interrupt. when cleared (zero), no con- nection between the analog comparator and the input capture function is given. to make the comparator trigger the timer/counter1 input capture interrupt, the ticie1 bit in the timer interrupt mask register (timsk) must be set (one).  bits 1,0 - acis1, acis0: analog comparator interrupt mode select these bits determine which comparator events that trigger the analog comparator interrupt. the different settings are shown in table 25. note: when changing the acis1/acis0 bits, the analog comparator interrupt must be disabled by clearing its interrupt enable bit in the acsr register. otherwise an interrupt can occur when the bits are changed. caution: using the sbi or cbi instruction on other bits than aci in this register, will write a one back into aci if it is read as set, thus clearing the flag. the analog comparator pins (pb2 and pb3) are also used as the txd1 and rxd1 pins for uart1. note that if the uart1 transceiver or receiver is enabled, the uart1 will override the settings in the ddrb register even if the analog comparator is enabled. therefore it is not recommended to use uart1 if the analog comparator is needed in the same application at the same time. see ? uarts ? on page 60 for more details. internal voltage reference atmega161 features an internal voltage reference with a nominal voltage of 1.22v. this reference is used for brown-out detection, and it can be used as an input to the analog comparator. voltage reference enable signals and start-up time the voltage reference has a start-up time that may influence on the way it should be used. the maximum start-up time is tbd. to save power, the reference is on during the following situations only: 1. when bod is enabled (by programming the boden fuse). 2. when the bandgap reference is connected to the analog comparator (by setting the ainbg bit in acsr). thus, when bod is not enabled, after setting the ainbg bit, the user must always allow the reference to start up before the output from the analog comparator is used. the bandgap reference uses approx. 10 a, and to reduce the power con- sumption in power-down mode, the user can turn off the reference when entering this mode. table 25. acis1/acis0 settings acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle 01reserved 1 0 comparator interrupt on falling output edge 1 1 comparator interrupt on rising output edge
atmega161(l) 72 interface to external memory with all the features the external memory interface provides, it is well suited to operate as an interface to memory devices such as external sram and flash, and peripherals as lcd-display, a/d, d/a etc. the control bits for the external mem- ory interface are located in two registers, the mcu control register ? mcucr and the extended mcu control register ? emcucr. mcu control register ? mcucr extended mcu control register ? emcucr  bit 7 mcucr ? sre: external sram enable when the sre bit is set (one), the external memory interface is enabled, and the pin functions ad0-7 (port a), a8-15 (port c), ale (port e), wr and rd (port d) are activated as the alternate pin functions. the sre bit overrides any pin direction settings in the respective data direction registers. see figure 51 ? figure 54 for description of the external memory pin functions. when the sre bit is cleared (zero), the external data memory interface is disabled, and the normal pin and data direction settings are used  bit 6..4 emcucr ? srl2, srl1, srl0: wait state page limit it is possible to configure different wait-states for different external memory addresses. the external memory address space can be divided in two pages with different wait-state bits. the srl2, srl1 and srl0 bits select the split of the pages, see table 27 and figure 50. as default the srl2, srl1 and srl0 bits are set to zero and the entire external mem- ory address space is treated as one page. when the entire sram address space is configured as one page, the wait- states are configured by the srw11 and srw10 bits.  bit 1 emcucr and bit 6 mcucr ? srw11, srw10: wait state select bits for upper page the srw11 and srw10 bits control the number of wait-states for the upper page of the external memory address space, see table 26. note that if the srl2, srl1 and srl0 bits are set to zero, the srw11 and srw10 bit settings will define the wait-state of the entire sram address space.  bit 3..2 emcucr ? srw01, srw00: wait state select bits for lower page the srw01 and srw00 bits control the number of wait-states for the lower page of the external memory address space, see table 26. note: n = 0 or 1 (lower/upper page). for further details of the timing and wait-states of the external memory interface, see figure 51 ? figure 54 how the setting of the srw bits affects the timing. bit 76543210 $35 ($55) sre srw10 se sm1 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $36 ($56) sm0 srl2 srl1 srl0 srw01 srw00 srw11 isc20 emcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 26. wait-states srwn1 srwn0 wait-states 0 0 no wait states 0 1 wait one cycle during read/write strobe 1 0 wait two cycles during read/write strobe 1 1 wait two cycles during read/write and wait one cycle before driving out new address
atmega161(l) 73 table 27. page limits with different settings of srl2..0 srl2 srl1 srl0 page limits 000 lower page = n/a upper page = $0460-$ffff 001 lower page = $0460-$1fff upper page = $2000-$ffff 010 lower page = $0460-$4fff upper page = $4000-$ffff 011 lower page = $0460-$5fff upper page = $6000-$ffff 100 lower page = $0460-$7fff upper page = $8000-$ffff 101 lower page = $0460-$9fff upper page = $a000-$ffff 110 lower page = $0460-$bfff upper page = $c000-$ffff 111 lower page = $0460-$dfff upper page = $e000-$ffff
atmega161(l) 74 figure 50. external memory with page select figure 51. external data memory cycles without wait state (srwn1 = 0 and srwn0 =0) note: srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page) the ale pulse in period t4 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t4 if ale is present (the next instruction accesses the ram). $0000 data memory $0460 external memory (0-63k x 8) $ffff internal memory srl[2..0] srw11 srw10 srw01 srw00 lower page upper page system clock ? ale wr rd data / address [7..0] data / address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t4 xx xx xx xx prev. addr. xx prev. data xx prev. data
atmega161(l) 75 figure 52. external data memory cycles with srwn1 = 0 and srwn0 = 1 note: srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page) the ale pulse in period t5 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t5 if ale is present (the next instruction accesses the ram). figure 53. external data memory cycles with srwn1 = 1 and srwn0 = 0 note: srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page) the ale pulse in period t6 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t6 if ale is present (the next instruction accesses the ram). figure 54. external data memory cycles with srwn1 = 1 and srwn0 = 1 note: srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page) the ale pulse in period t7 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t7 if ale is present (the next instruction accesses the ram). system clock ? ale wr rd data / address [7..0] data / address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t5 xx xx xx xx prev. addr. xx prev. data xx prev. data t4 system clock ? ale wr rd data / address [7..0] data / address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t6 xx xx xx xx prev. addr. xx prev. data xx prev. data t4 t5 system clock ? ale wr rd data / address [7..0] data / address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t7 xx xx xx xx prev. addr. xx prev. data xx prev. data t4 t5 t6
atmega161(l) 76 using the external memory interface the interface consists of:  port a: multiplexed low-order address bus and data bus  port c: high-order address bus  the ale-pin: address latch enable  the rd and wr -pin: read and write strobes. the external memory interface is enabled by setting the sre ? external sram enable bit of the mcucr ? mcu control register, and will over-ride the setting of the data direction register ddra, ddrd and ddre. when the sre bit is cleared (zero), the external memory interface is disabled, and the normal pin and data direction settings are used. when sre is low, the address space above the internal sram boundary is not mapped into the internal sram, as in avr parts not hav- ing external memory interface. when ale goes from high to low, there is a valid address on port a. ale is low during a data transfer. rd and wr are active when accessing the external memory only. when the external memory interface is enabled, the ale signal may have short pulses when accessing the internal ram, but the ale signal is stable when accessing the external memory. figure 55 sketches how to connect an external sram to the avr using 8 latches which are transparent when g is high. figure 55. external sram connected to the avr for details in the timing for the sram interface, please refer to figure 84 ? figure 87 and table 49 ? table 56. i/o-ports all avr ports have true read-modify-write functionality when used as general digital i/o ports. this means that the direc- tion of one port pin can be changed without unintentionally changing the direction of any other pin with the sbi and cbi instructions. the same applies for changing drive value (if configured as output) or enabling/disabling of pull-up resistors (i f configured as input). port a port a is an 8-bit bi-directional i/o port. three i/o memory address locations are allocated for the port a, one each for the data register ? porta, $1b($3b), data direction register ? ddra, $1a($3a) and the port a input pins ? pina, $19($39). the port a input pins address is read only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port a output buffers can sink 20 ma and thus drive led dis- plays directly. when pins pa0 to pa7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. d[7:0] a[7:0] a[15:8] rd wr sram dq g port a ale port c rd wr avr
atmega161(l) 77 the port a pins have alternate functions related to the optional external memory interface. port a can be configured to be the multiplexed low-order address/data bus during accesses to the external data memory. in this mode, port a has internal pull-up resistors. when port a is set to the alternate function by the sre ? external sram enable bit in the mcucr ? mcu control regis- ter, the alternate settings override the data direction register. port a data register ? porta port a data direction register ? ddra port a input pins address ? pina the port a input pins address ? pina ? is not a register, and this address enables access to the physical value on each port a pin. when reading porta the port a data latch is read, and when reading pina, the logical values present on the pins are read. port a as general digital i/o all 8 pins in port a have equal functionality when used as digital i/o pins. pan, general i/o pin: the ddan bit in the ddra register selects the direction of this pin, if ddan is set (one), pan is con- figured as an output pin. if ddan is cleared (zero), pan is configured as an input pin. if portan is set (one) when the pin configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, the portan has to be cleared (zero) or the pin has to be configured as an output pin. the port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. n: 7,6 ? 0, pin number. port a schematics note that all port pins are synchronized. the synchronization latch is however, not shown in the figure. bit 76543210 $1b ($3b) porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 porta read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $1a ($3a) dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 ddra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $19 ($39) pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 pina read/write rrrrrrrr initial value hi-z hi-z hi-z hi-z hi-z hi-z hi-z hi-z table 28. ddan effects on port a pins ddan portan i/o pull-up comment 0 0 input no tri-state (hi-z) 0 1 input yes pan will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
atmega161(l) 78 figure 56. port a schematic diagrams (pins pa0 - pa7) port b port b is an 8-bit bi-directional i/o port. three i/o memory address locations are allocated for the port b, one each for the data register ? portb, $18($38), data direction register ? ddrb, $17($37) and the port b input pins ? pinb, $16($36). the port b input pins address is read only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port b output buffers can sink 20 ma and thus drive led dis- plays directly. when pins pb0 to pb7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. the port b pins with alternate functions are shown in the following table: table 29. port b pins alternate functions port pin alternate functions pb0 oc0 (timer/counter 0 compare match output) /t0 (timer/counter 0 external counter input) pb1 oc2 (timer/counter 2 compare match output) /t1 (timer/counter 1 external counter input) pb2 rxd1 (uart1 input line) /ain0 (analog comparator positive input) pb3 txd1 (uart1 output line) /ain1 (analog comparator negative input) pb4 ss (spi slave select input) pb5 mosi (spi bus master output/slave input) pb6 miso (spi bus master input/slave output) pb7 sck (spi bus serial clock)
atmega161(l) 79 when the pins are used for the alternate function the ddrb and portb register has to be set according to the alternate function description. port b data register ? portb port b data direction register ? ddrb port b input pins address ? pinb the port b input pins address ? pinb ? is not a register, and this address enables access to the physical value on each port b pin. when reading portb, the port b data latch is read, and when reading pinb, the logical values present on the pins are read. port b as general digital i/o all 8 pins in port b have equal functionality when used as digital i/o pins. pbn, general i/o pin: the ddbn bit in the ddrb register selects the direction of this pin, if ddbn is set (one), pbn is con- figured as an output pin. if ddbn is cleared (zero), pbn is configured as an input pin. if portbn is set (one) when the pin configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, the portbn has to be cleared (zero) or the pin has to be configured as an output pin. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running n: 7,6 ? 0, pin number. bit 76543210 $18 ($38) portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $17 ($37) ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $16 ($36) pinb7 pinb6 pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 pinb read/write rrrrrrrr initial value hi-z hi-z hi-z hi-z hi-z hi-z hi-z hi-z table 30. ddbn effects on port b pins ddbn portbn i/o pull-up comment 0 0 input no tri-state (hi-z) 0 1 input yes pbn will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
atmega161(l) 80 alternate functions of port b the alternate pin configuration is as follows:  sck - port b, bit 7 sck: master clock output, slave clock input pin for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb7. when the spi is enabled as a master, the data direction of this pin is con- trolled by ddb7. when the pin is forced to be an input, the pull-up can still be controlled by the portb7 bit. see the description of the spi port for further details.  miso - port b, bit 6 miso: master data input, slave data output pin for spi channel. when the spi is enabled as a master, this pin is configured as an input regardless of the setting of ddb6. when the spi is enabled as a slave, the data direction of this pin is controlled by ddb6. when the pin is forced to be an input, the pull-up can still be controlled by the portb6 bit. see the description of the spi port for further details.  mosi - port b, bit 5 mosi: spi master data output, slave data input for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb5. when the spi is enabled as a master, the data direction of this pin is con- trolled by ddb5. when the pin is forced to be an input, the pull-up can still be controlled by the portb5 bit. see the description of the spi port for further details.  ss - port b, bit 4 ss : slave port select input. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb5. as a slave, the spi is activated when this pin is driven low. when the spi is enabled as a master, the data direc- tion of this pin is controlled by ddb5. when the pin is forced to be an input, the pull-up can still be controlled by the portb5 bit. see the description of the spi port for further details.  txd1/ain1 - port b, bit 3 ain1, analog comparator negative input. this pin also serves as the negative input of the on-chip analog comparator. txd1, transmit data (data output pin for the uart1). when the uart1 transmitter is enabled, this pin is configured as an output regardless of the value of ddrb3.  rxd1/ain0 - port b, bit 2 ain0, analog comparator positive input. this pin also serves as the positive input of the on-chip analog comparator. rxd1, receive data (data input pin for the uart1). when the uart1 receiver is enabled this pin is configured as an input regardless of the value of ddrb2. when the uart1 forces this pin to be an input, a logical one in portb2 will turn on the internal pull-up.  oc2/t1 - port b, bit 1 t1, timer/counter1 counter source. see the ? timer/counter1. ? on page 45 for further details. oc2, output compare match output: the pb1 pin can serve as an external output when the timer/counter2 compare matches. the pb1 pin has to be configured as an output (ddb1 set (one)) to serve this function. see ? 8-bit tim- ers/counters t/c0 and t/c2 ? on page 37 for further details. the oc2 pin is also the output pin for the pwm mode timer function.  oc0/t0 - port b, bit 0 t0: timer/counter0 counter source. see the ? 8-bit timers/counters t/c0 and t/c2 ? on page 37 further details. oc0, output compare match output: the pb0 pin can serve as an external output when the timer/counter0 compare matches. the pb0 pin has to be configured as an output (ddb0 set (one)) to serve this function. see ? 8-bit tim- ers/counters t/c0 and t/c2 ? on page 37 for further details, and how to enable the output. the oc0 pin is also the output pin for the pwm mode timer function. port b schematics note that all port pins are synchronized. the synchronization latches are however, not shown in the figures.
atmega161(l) 81 figure 57. port b schematic diagram (pins pb0 and pb1) figure 58. port b schematic diagram (pin pb2) pbn ddbn portbn comp. match x comx0 comx1 wp: wd: rl: rp: rd: write portb write ddrb read portb latch read portb pin read ddrb focx pwmx n: 0,1 x: 0,2 csn2 csn1 csn0 da ta bus d d q q reset reset c c wd wp rd mos pull- up pb2 rxd1 rxen1 wp: wd: rl: rp: rd: rxd1: rxen1: write portb write ddrb read portb latch read portb pin read ddrb uart1 receive data uart1 receive enable ddb2 portb2 rl rp ain0: analog comparator positive input ain0
atmega161(l) 82 figure 59. port b schematic diagram (pin pb3) figure 60. port b schematic diagram (pin pb4) da ta bus d d q q reset reset c c wd wp rd rp rl mos pull- up pb3 r r wp: wd: rl: rp: rd: txd1: txen1: write portb write ddrb read portb latch read portb pin read ddrb uart1 transmit data uart1 transmit enable ddb3 portb3 txen1 txd1 ain1 ain1: analog comparator negative input da ta bus d d q q reset reset c c wd wp rd mos pull- up pb4 spi ss mstr spe wp: wd: rl: rp: rd: mstr: spe: write portb write ddrb read portb latch read portb pin read ddrb spi master enable spi enable ddb4 portb4 rl rp
atmega161(l) 83 figure 61. port b schematic diagram (pin pb5) figure 62. port b schematic diagram (pin pb6) data bus d d q q reset reset c c wd wp rd mos pull- up pb5 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb5 portb5 spe mstr spi master out spi slave in rl rp da ta bus d d q q reset reset c c wd wp rd mos pull- up pb6 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb6 portb6 spe mstr spi slave out spi master in rl rp
atmega161(l) 84 figure 63. port b schematic diagram (pin pb7) port c port c is an 8-bit bi-directional i/o port. three i/o memory address locations are allocated for the port c, one each for the data register ? portc, $15($35), data direction register ? ddrc, $14($34) and the port c input pins ? pinc, $13($33). the port c input pins address is read only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port c output buffers can sink 20 ma and thus drive led dis- plays directly. when pins pc0 to pc7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. the port c pins have alternate functions related to the optional external memory interface. port c can be configured to be the high-order address byte during accesses to external data memory. when port c is set to the alternate function by the sre ? external sram enable ? bit in the mcucr ? mcu control reg- ister, the alternate settings override the data direction register. data bus d d q q reset reset c c wd wp rd mos pull- up pb7 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb7 portb7 spe mstr spi cllock out spi clock in rl rp
atmega161(l) 85 port c data register ? portc port c data direction register ? ddrc port c input pins address ? pinc the port c input pins address ? pinc ? is not a register, and this address enables access to the physical value on each port c pin. when reading portc, the port c data latch is read, and when reading pinc, the logical values present on the pins are read. port c as general digital i/o all 8 pins in port c have equal functionality when used as digital i/o pins. pcn, general i/o pin: the ddcn bit in the ddrc register selects the direction of this pin, if ddcn is set (one), pcn is con- figured as an output pin. if ddcn is cleared (zero), pcn is configured as an input pin. if portcn is set (one) when the pin configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, portcn has to be cleared (zero) or the pin has to be configured as an output pin.the port c pins are tri-stated when a reset condition becomes active, even if the clock is not running. n: 7, 6, ? 0, pin number port c schematics note that all port pins are synchronized. the synchronization latch is however, not shown in the figure. bit 76543210 $15 ($35) portc7 portc6 portc5 portc4 portc3 portc2 portc1 portc0 portc read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $14 ($34) ddc7 ddc6 ddc5 ddc4 ddc3 ddc2 ddc1 ddc0 ddrc read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $13 ($33) pinc7 pinc6 pinc5 pinc4 pinc3 pinc2 pinc1 pinc0 pinc read/write rrrrrrrr initial value hi-z hi-z hi-z hi-z hi-z hi-z hi-z hi-z table 31. ddcn effects on port c pins ddcn portcn i/o pull-up comment 0 0 input no tri-state (hi-z) 0 1 input yes pcn will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
atmega161(l) 86 figure 64. port c schematic diagram (pins pc0 - pc7) port d port d is an 8 bit bi-directional i/o port with internal pull-up resistors. three i/o address locations are allocated for the port d, one each for the data register ? portd, $12($32), data direction register ? ddrd, $11($31) and the port d input pins ? pind, $10($30). the port d input pins address is read only, while the data register and the data direction register are read/write. the port d output buffers can sink 20 ma. as inputs, port d pins that are externally pulled low will source current if the pull - up resistors are activated. some port d pins have alternate functions as shown in the following table: when the pd5 pin is used for the alternate function (oc1a) the ddrd and portd register has to be set according to the alternate function description. table 32. port d pins alternate functions port pin alternate function pd0 rxd0 (uart0 input line) pd1 txd0 (uart0 output line) pd2 int0 (external interrupt 0 input) pd3 int1 (external interrupt 1 input) pd3 tosc1 (rtc oscillator timer/counter 2) pd5 tosc2 (rtc oscillator timer/counter 2)/oc1a (timer/counter1 output comparea match output) pd6 wr (write strobe to external memory) pd7 rd (read strobe to external memory)
atmega161(l) 87 port d data register ? portd port d data direction register ? ddrd port d input pins address ? pind the port d input pins address ? pind ? is not a register, and this address enables access to the physical value on each port d pin. when reading portd, the port d data latch is read, and when reading pind, the logical values present on the pins are read. port d as general digital i/o pdn, general i/o pin: the dddn bit in the ddrd register selects the direction of this pin. if dddn is set (one), pdn is con- figured as an output pin. if dddn is cleared (zero), pdn is configured as an input pin. if portdn is set (one) when configured as an input pin the mos pull-up resistor is activated. to switch the pull-up resistor off, the portdn has to be cleared (zero) or the pin has to be configured as an output pin. the port d pins are tri-stated when a reset condition becomes active, even if the clock is not running. n: 7,6 ? 0, pin number. alternate functions of port d  rd - port d, bit 7 rd is the external data memory read control strobe.  wr - port d, bit 6 wr is the external data memory write control strobe.  oc1 - port d, bit 5 oc1, output compare match output: the pd5 pin can serve as an external output when the timer/counter1 compare matches. the pd5 pin has to be configured as an output (ddd5 set (one)) to serve this function. see ? timer/counter1. ? on page 45 for further details, and how to enable the output. the oc1 pin is also the output pin for the pwm mode timer function. bit 76543210 $12 ($32) portd7 portd6 portd5 portd4 portd3 portd2 portd1 portd0 portd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $11 ($31) ddd7 ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 ddrd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $10 ($30) pind7 pind6 pind5 pind4 pind3 pind2 pind1 pind0 pind read/write rrrrrrrr initial value hi-z hi-z hi-z hi-z hi-z hi-z hi-z hi-z table 33. dddn bits on port d pins dddn portdn i/o pull-up comment 0 0 input no tri-state (hi-z) 0 1 input yes pdn will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
atmega161(l) 88  tosc1/tosc2 - port d, bit 5 and 4 when the as2 bit in assr is set (one) to enable asynchronous clocking of timer/counter2, pins pd5 and pd4 are discon- nected from the port. in this mode, a crystal oscillator is connected to the pins, and the pins can not be used as i/o pins.  int1 - port d, bit 3 int1, external interrupt source 1: the pd3 pin can serve as an external interrupt source to the mcu. see ? mcu control register ? mcucr ? on page 32 for further details.  int0 - port d, bit 2 int0, external interrupt source 0: the pd2 pin can serve as an external interrupt source to the mcu. see ? mcu control register ? mcucr ? on page 32 for further details.  txd0 - port d, bit 1 transmit data (data output pin for the uart0). when the uart0 transmitter is enabled, this pin is configured as an output regardless of the value of ddrd1.  rxd0 - port d, bit 0 receive data (data input pin for the uart0). when the uart receiver is enabled this pin is configured as an input regard- less of the value of ddrd0. when the uart0 forces this pin to be an input, a logical one in portd0 will turn on the internal pull-up. port d schematics note that all port pins are synchronized. the synchronization latches are however, not shown in the figures. figure 65. port d schematic diagram (pin pd0) da ta bus d d q q reset reset c c wd wp rd mos pull- up pd0 rxd0 rxen0 wp: wd: rl: rp: rd: rxd0: rxen0: write portd write ddrd read portd latch read portd pin read ddrd uart0 receive data uart0 receive enable ddd0 portd0 rl rp
atmega161(l) 89 figure 66. port d schematic diagram (pin pd1) figure 67. port d schematic diagram (pins pd2 and pd3) da ta bus d d q q reset reset c c wd wp rd rp rl mos pull- up pd1 r r wp: wd: rl: rp: rd: txd0: txen0: write portd write ddrd read portd latch read portd pin read ddrd uart0 transmit data uart0 transmit enable ddd1 portd1 txen0 txd0 wp: wd: rl: rp: rd: n: m: write portd write ddrd read portd latch read portd pin read ddrd 2, 3 0, 1
atmega161(l) 90 figure 68. port d schematic diagram (pin pd4) figure 69. port d schematic diagram (pin pd5) da ta bus d d q q reset reset c c wd wp rd mos pull- up pd4 r r wp: wd: rl: rp: rd: as2: write portd write ddrd read portd latch read portd pin read ddrd asynch select t/c2 ddd4 portd4 rl rp as2 t/c2 osc amp input comp. match 1a foc1a pwm10 pwm11 wp: wd: rl: rp: rd: as2 write portd write ddrd read portd latch read portd pin read ddrd asynch select t/c2
atmega161(l) 91 figure 70. port d schematic diagram (pin pd6) figure 71. port d schematic diagram (pin pd7) wp: wd: rl: rp: rd: we: sre: write portd write ddrd read portd latch read portd pin read ddrd write enable external sram enable wp: wd: rl: rp: rd: re: sre: write portd write ddrd read portd latch read portd pin read ddrd read enable external sram enable
atmega161(l) 92 port e port e is a 3 bit bi-directional i/o port with internal pull-up resistors. three i/o address locations are allocated for the port e, one each for the data register ? porte, $07($27), data direc- tion register ? ddre, $06($26) and the port e input pins ? pine, $05($25). the port e input pins address is read only, while the data register and the data direction register are read/write. the port e output buffers can sink 20 ma. as inputs, port e pins that are externally pulled low will source current if the pull - up resistors are activated. port e pins have alternate functions as shown in the following table: when the pe1 pin is used for the alternate function the ddre and porte register has to be set according to the alternate function description. port e data register ? porte port e data direction register ? ddre port e input pins address ? pine the port e input pins address ? pine ? is not a register, and this address enables access to the physical value on each port e pin. when reading porte, the port e data latch is read, and when reading pine, the logical values present on the pins are read. port e as general digital i/o pen, general i/o pin: the dden bit in the ddre register selects the direction of this pin. if dden is set (one), pen is con- figured as an output pin. if dden is cleared (zero), pen is configured as an input pin. if porten is set (one) when configured as an input pin the mos pull-up resistor is activated. to switch the pull-up resistor off the porten has to be cleared (zero) or the pin has to be configured as an output pin.the port e pins are tri-stated when a reset condition becomes active, even if the clock is not running. table 34. port e pins alternate functions port pin alternate function pe0 icp (input capture pin timer/counter1)/int2 (external interrupt 2 input) pe1 oc1b (timer/counter1 output compareb match output) pe2 ale (address latch enable, external memory) bit 76543210 $07 ($27) - - - - - porte2 porte1 porte0 porte read/write r r r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $06 ($26) - - - - - dde2 dde1 dde0 ddre read/write r r r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $05 ($25) - - - - - pine2 pine1 pine0 pine read/write rrrrrrrr initial value 0 0 0 0 0 hi-z hi-z hi-z
atmega161(l) 93 n: 2,1,0, pin number. alternate functions of port e  oc1b - port e, bit 2 oc1b, output compare match output: the pe2 pin can serve as an external output when the timer/counter1 compare matches. the pe2 pin has to be configured as an output (dde2 set (one)) to serve this function. see ? timer/counter1. ? on page 45 for further details. the oc1b pin is also the output pin for the pwm mode timer function.  ale - port e, bit 1 ale: when the external memory is enabled, the pe1 pin serves as the dress latch enable. note that enabling of external memory will override both the direction and port value. see ? interface to external memory ? on page 72 for a detailed description.  icp/int2 - port e, bit 0 icp, input capture pin: the pe0 pin can serve as the input capture source for timer/counter 1. see page 49 for a detailed description. int2, external interrupt source 2: the pe0 pin can serve as an external interrupt source to the mcu. see ? extended mcu control register ? emcucr ? on page 33 for further details. port e schematics figure 72. port e schematic diagram (pin pe0) table 35. dden bits on port e pins dden porten i/o pull-up comment 0 0 input no tri-state (hi-z) 0 1 input yes pen will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output data bus d d q q reset reset c c wd wp rd mos pull- up pe0 r r wp: wd: rl: rp: rd: acic: aco: write porte write ddre read porte latch read porte pin read ddre comparator ic enable comparator output dde0 porte0 noise canceler edge select icf1 icnc1 ices1 0 1 acic aco rl rp d q c r porte0 isc2 '1' int2 sw clear hw clear
atmega161(l) 94 figure 73. port e schematic diagram (pin pe1) figure 74. port e schematic diagram (pin pe2 da ta bus d d q q reset reset c c wd wp rd mos pull- up pe1 r r wp: wd: rl: rp: rd: sre: write porte write ddre read porte latch read porte pin read ddre dde1 porte1 rl rp sre ale xram enable ale: ale pulse from xram pe2 dde2 porte2 comp. match 1b com1b0 com1b1 wp: wd: rl: rp: rd: write porte write ddre read porte latch read porte pin read ddre foc1b pwm10 pwm11
atmega161(l) 95 memory programming boot loader support the atmega161 provides a mechanism for downloading and uploading program code by the mcu itself. this feature allows flexible application software updates, controlled by the mcu using a flash-resident boot loader program. the atmega161 flash memory is organized in two main sections; 1. the application code section (address $0000 - $1dff) 2. the boot loader section/boot block (address $1e00 - $1fff) figure 75. memory sections boot loader program can use any available data interface and associated protocol, such as uart serial bus interface, to input or output program code, and write (program) that code into the flash memory, or read the code from the program memory. the program flash memory is divided into pages that contains 128 bytes each. the boot loader flash section occupies 8 pages from $1e00 to $1fff by 16 bit words. $0000 $1fff program memory application code section (7.5k x 16) $1e00 $1dff boot loader section (512 x 16)
atmega161(l) 96 the store program memory (spm) instruction can access the entire flash, but it can only be executed from the boot loader flash section. if no boot loader capability is needed, the entire flash is available for application code. the atmega161 has two separate sets of boot lock bits which can be set independently. this gives the user a unique flexibility to select different levels of protection. the user can select:  to protect the entire flash from a software update by the boot loader program.  to only protect the boot loader section from a software update by the boot loader program.  to only protect the application code section from a software update by the boot loader program.  allowing software update in the entire flash see table 36 and table 37 for further details. the boot lock bits can be set in software and in serial or parallel programming mode, but they can only be cleared by a chip erase command. note: ? 1 ? means unprogrammed, ? 0 ? means programmed note: ? 1 ? means unprogrammed, ? 0 ? means programmed entering the boot loader program entering the boot loader takes place by a jump or call from the application program. this may be initiated by some trigger such as a command received via uart or spi interface. alternatively, the boot reset fuse (bootrst) can be pro- grammed so that the reset vector is pointing to address $1e00 after a reset. in this case, the boot loader is started after the reset. after the application code is loaded, the program can start executing the application code. note that the fuses cannot be changed by the mcu itself. this means that once the boot reset fuse is programmed, the reset vector will always point to the boot loader reset and the fuse can only be changed through the serial or parallel programming inter- face. the bootrst fuse can also be locked by programming lb1. when lb1 is programmed it is not possible to change the bootrst fuse unless a chip erase command is performed first. note: ? 1 ? means unprogrammed, ? 0 ? means programmed table 36. boot lock bit0 protection modes (application section) blb0 mode blb01 blb02 protection 1 1 1 no restrictions for spm, lpm in the application code section (address $0000 - $1dff) 2 0 1 it is not allowed to update the application code section (address $0000 - $1dff) by spm. 3 0 0 lpm read and spm write prohibited in the application code section (address $0000 - $1dff) 410 it is not allowed to read program code located in the application code section (address $0000 - $1dff) by lpm table 37. boot lock bit1 protection modes (boot loader section) blb1 mode blb11 blb12 protection 1 1 1 no restrictions for spm, lpm in the boot loader section (address $1e00 - $1fff) 2 0 1 it is not allowed to update the boot loader section (address $1e00 - $1fff) by spm 3 0 0 lpm and spm prohibited in the boot loader section (address $1e00 - $1fff) 410 it is not allowed to read program code located in the boot loader section (address $1e00 - $1fff) by lpm table 38. boot reset fuse, bootrst bootrst reset address 1 reset vector = application reset (address $0000) 0 reset vector = boot loader reset (address $1e00)
atmega161(l) 97 capabilities of the boot loader the program code within the boot loader section has the capability to read from and write into the entire flash, including the boot loader memory. this allows the user to update both the application code and the boot loader code that handles the software update. the boot loader can thus even modify itself, and it can also erase itself from the code if the feature is not needed anymore. special care must be taken if the user allows the boot loader section to be updated by leaving boot lock bit11 unprogrammed. an accidental write to the boot loader itself can corrupt the entire boot loader, and further soft- ware updates might be impossible. if it is not needed to change the boot loader software itself, it is recommended to program the boot lock bit11 to protect the boot loader software from software changes. self-programming the flash programming of the flash is executed one page at a time. the flash page must be erased first for correct programming. the general write lock (lock bit 2) does not control the programming of the flash memory by spm instruction. similarly, the read/write lock (lock bit 1) does not control reading nor writing by lpm/spm, if it is attempted. the program memory can only be updated page by page, not word by word. one page is 128 bytes (64 words). the pro- gram memory will be modified by first performing page erase, then filling the temporary page buffer one word at a time using spm, and then executing page write. if only part of the page needs to be changed, the other parts must be stored (for example in the temporary page buffer) before the erase, and then be rewritten. the temporary page buffer can be accessed in a random sequence. the cpu is halted both during page erase and during page write. it is essential that the page address used in both the page erase and page write operation is addressing the same page.  setting the boot loader lock bits by spm to set the boot loader lock bits, write the desired data to r0, write "1001" to spmcr and execute spm within four clock cycles after writing spmcr. the only accessible lock bits are the boot lock bits that may prevent the application and boot loader section from any software update by the mcu. see table 36 and table 37 how the different settings of the boot loader bits affect the flash access. if bit5 - bit2 in r0 is cleared (zero), the corresponding boot lock bit will be programmed if a spm instruction is executed within four cycles after blbset and spmen are set in spmcr.  performing page erase by spm to execute page erase, set up the address in the z pointer, write "0011" to spmcr and execute spm within four clock cycles after writing spmcr. the data in r1 and r0 are ignored. the page address must be written to z13:z7. other bits in the z pointer will be ignored during this operation.  fill the temporary buffer to write an instruction word, set up the address in the z pointer and data in r1:r0, write "0001" to spmcr and execute spm within four clock cycles after writing spmcr. the content of z6:z1 is used to address the data in the temporary buffer. z13:z7 must point to the page that is supposed to be written.  perform a page write to execute page write, set up the address in the z pointer, write "0101" to spmcr and execute spm within four clock cycles after writing spmcr. the data in r1 and r0 are ignored. the page address must be written to z13:z7. during this operation, z6:z0 must be zero to ensure that the page is written correctly. bit 76543210 - - blb12 blb11 blb02 blb01 - - r0
atmega161(l) 98 addressing the flash during self-programming the z pointer is used to address the spm commands. z15:z14 always ignored z13:z7 page select, for page erase, page write z6:z1 word select, for filling temp buffer (must be zero during page write operation) z0 should be zero for all spm commands, byte select for the lpm instruction. the only operation that does not use the z pointer is setting the boot loader lock bits. the content of the z pointer is ignored and will have no effect on the operation. note that the page erase and page write operation is addressed independently. therefore it is of major importance that the boot loader software addresses the same page in both the page erase and page write operation. the lpm instruction does also use the z pointer to store the address. since this instruction does address the flash byte by byte, also the lsb (bit z0) of the z pointer is used. see page 16 for a detailed description. accidental writing into flash program by the spm instruction is prevented by setting up a "spm enable time window". all accesses are executed by first setting i/o bits, and then executing spm within four clock cycles. the i/o register that con- trols the spm accesses is defined as follows: store program memory control register ? spmcr the store program memory control register contains the control bits needed to control the programming of the flash from internal code execution.  bit 7..4 - res: reserved bits these bits are reserved bits in the atmega161 and always read as zero.  bit 3 - blbset: boot lock bit set if this bit is set at the same time as spmen, the next spm instruction within four clock cycles sets boot lock bits, according to the data in r0. the data in r1 and the address in the z pointer are ignored. the blbset bit will auto-clear upon comple- tion of lock bit set, or if no spm instruction is executed within four clock cycles. the cpu is halted during lock bit setting. only a chip erase can clear the lock bits. an lpm instruction within four cycles after blbset and spmen are set in the spmcr register, will put either the lock-bits or the fuse-bits (depending od the z0 in the z-pointer) into the destination register. see ? reading the fuse- and lock bits from software ? on page 99 for details.  bit 2 - pgwrt: page write if this bit is set at the same time as spmen, the next spm instruction within four clock cycles executes page write, with the data stored in the temporary buffer. the page address is taken from the high part of the z pointer. the data in r1 and r0 are ignored. the pgwrt bit will auto-clear upon completion of a page write, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page write operation.  bit 1 - pgers: page erase if this bit is set at the same time as spmen, the next spm instruction within four clock cycles executes page erase. the page address is taken from the high part of the z pointer. the data in r1 and r0 are ignored. the pgers bit will auto-clear upon completion of a page erase, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page erase operation. bit 151413121110 9 8 $1f ($1f) z15 z14 z13 z12 z11 z10 z9 z8 zh $1e ($1e) z7 z6 z5 z4 z3 z2 z1 z0 zl 76543210 bit 76543210 $37 ($57) - - blbset pgwrt pgers spmen spmcr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
atmega161(l) 99  bit 0 - spmen: store program memory enable this bit enables the spm instruction for the next four clock cycles. if set together with either blbset, pgwrt or pgers, the following spm instruction will have a special meaning, see description above. if only spmen is set, the following spm instruction will store the value in r1:r0 in the temporary page buffer addressed by the z pointer. the lsb of the z pointer is ignored. the spmen bit will auto-clear upon completion of an spm instruction, or if no spm instruction is executed within four clock cycles. writing any other combination that "1001", "0101", "0011" or "0001" in the lower four bits, or writing to the i/o register when any bits are set, will have no effect. eeprom write prevents writing to spmcr note that an eeprom write operation will block all software programming to flash. reading the fuses and lockbits from software will also be prevented during the eeprom write operation. it is recommended that the user checks the status bit (eewe) in the eecr register and verifies that the bit is cleared before writing to the spmcr register. reading the fuse- and lock bits from software it is possible to read both the fuse and lock bits from software. to read the lock bits, load the z-pointer with $0001 and set the blbset and spmen bits in spmcr. if an lpm instruction is executed within three cpu cycles after the blbset and spmen bits are set in spmcr, the lock bits will be written to the destination register. the blbset and spmen bits will auto-clear upon completion of reading the lock bits or if no lpm/spm instruction is executed within three/four cpu cycles. when blbset and spmen are cleared, lpm will work as described in ? constant addressing using the lpm instruction ? on page 16 and in the instruction set manual. the algorithm for reading the fuse bits is similar to the one described above for reading the lock bits. but when reading the fuse bits, load $0000 in the z-pointer. when an lpm instruction is executed within three cycles after the blbset and spmen bits are set in the spmcr, the fuse-bits can be read in the destination register as shown below. fuse- and lock bits that are programmed, will be read as zero. bit 76543210 - - blb12 blb11 blb02 blb01 lb2 lb1 r0/rd bit 7654 3210 - bootrst spien bodlevel boden cksel[2] cksel[1] cksel[0] r0/rd
atmega161(l) 100 program memory lock bits the atmega161 mcu provides six lock bits which can be left unprogrammed ( ? 1 ? ) or can be programmed ( ? 0 ? ) to obtain the additional features listed in table 39. the lock bits can only be erased to ? 1 ? with the chip erase command. note: 1. program the fuse bits before programming the lock bits. fuse bits the atmega161 has seven fuse bits, bootrst, spien, bodlevel, boden and cksel [2:0].  when bootrst is programmed ( ? 0 ? ), the reset vector is set to address $1e00 which is the first address location in the boot loader section of the flash. if the bootrst is unprogrammed ( ? 1 ? ), the reset vector is set to address $0000. default value is unprogrammed ( ? 1 ? ).  when the spien fuse is programmed ( ? 0 ? ), serial program and data downloading is enabled. default value is programmed ( ? 0 ? ). the spien fuse is not accessible in serial programming mode.  the bodlevel fuse selects the brown-out detection level and changes the start-up times. see ? brown-out detection ? on page 27. default value is unprogrammed ( ? 1 ? ).  when the boden fuse is programmed ( ? 0 ? ), the brown-out detector is enabled. see ? brown-out detection ? on page 27. default value is unprogrammed ( ? 1 ? ).  cksel2..0: see table 4, ? reset delay selections, ? on page 25, for which combination of cksel2..0 to use. default value is ? 010 ? . the status of the fuse bits is not affected by chip erase. note that the fuse bits are locked if lock bit1 (lb1) or lock bit2 (lb2) is programmed. program the fuse bits before programming the lock bits. table 39. lock bit protection modes memory lock bits protection type lb mode lb1 lb2 1 1 1 no memory lock features enabled 201 further programming of the flash and eeprom is disabled in parallel and serial programming mode. the fuse bits are locked in both serial and parallel programming mode. (1) 300 further programming and verification of the flash and eeprom is disabled in parallel and serial programming mode. the fuse bits are locked in both serial and parallel programming mode. (1) . blb0 mode blb01 blb02 111 no memory lock features on spm and lpm in application code section (address $0000 - $1dff). 2 0 1 spm prohibited in the application code section (address $0000 - $1dff) 3 0 0 lpm and spm prohibited in the application code section (address $0000 - $1dff) 4 1 0 lpm prohibited in the application code section (address $0000 - $1dff) blb1 mode blb11 blb12 1 1 1 no memory lock features on spm and lpm in boot loader section (address $1e00 - $1fff). 2 0 1 spm prohibited in the boot loader section (address $1e00 - $1fff) 3 0 0 lpm and spm prohibited in the boot loader section (address $1e00 - $1fff) 4 1 0 lpm prohibited in the boot loader section (address $1e00 - $1fff)
atmega161(l) 101 signature bytes all atmel microcontrollers have a three-byte signature code which identifies the device. this code can be read in both serial and parallel mode. the three bytes reside in a separate address space, and for the atmega161 they are: 1. $000: $1e (indicates manufactured by atmel) 2. $001: $94 (indicates 16kb flash memory) 3. $002: $01 (indicates atmega161 device when $001 is $94) programming the flash and eeprom atmel ? s atmega161 offers 16k bytes of in-system reprogrammable flash program memory and 512 bytes of eeprom data memory. the atmega161 is normally shipped with the on-chip flash program and eeprom data memory arrays in the erased state (i.e. contents = $ff) and ready to be programmed. this device supports a high-voltage (12v) parallel programming mode and a low-voltage serial programming mode. the +12v is used for programming enable only, and no current of sig- nificance is drawn by this pin. the serial programming mode provides a convenient way to download the program and data into the atmega161 inside the user ? s system. the program memory array on the atmega161 is organized as 128 pages of 128 bytes each. when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be programmed simulta- neously in either programming mode. the eeprom data memory array on the atmega161 is programmed byte-by-byte in either programming mode. an auto- erase cycle is provided with the self-timed eeprom programming operation in the serial programming mode. during programming, the supply voltage must be in accordance with table . parallel programming this section describes how to parallel program and verify flash program memory, eeprom data memory, lock bits and fuse bits in the atmega161. pulses are assumed to be at least 500ns unless otherwise noted. signal names in this section, some pins of the atmega161 are referenced by signal names describing their functionality during parallel programming, see figure 76 and table 41. pins not described in the following table are referenced by pin names. the xa1/xa0 pins determine the action executed when the xtal1 pin is given a positive pulse. the bit coding are shown in table 42. when pulsing wr or oe , the command loaded determines the action executed. the command is a byte where the differ- ent bits are assigned functions as shown in table 43. table 40. supply voltage during programming part serial programming parallel programming atmega161l 2.7 - 5.5v 4.5 - 5.5v atmega161 4.0 - 5.5v 4.5 - 5.5v
atmega161(l) 102 figure 76. parallel programming table 41. pin name mapping signal name in programming mode pin name i/o function rdy/bsy pd1 o 0: device is busy programming, 1: device is ready for new command oe pd2 i output enable (active low) wr pd3 i write pulse (active low) bs1 pd4 i byte select 1 ( ? 0 ? selects low byte, ? 1 ? selects high byte) xa0 pd5 i xtal action bit 0 xa1 pd6 i xtal action bit 1 pagel pd7 i program memory page load bs2 pa0 i byte select 2 (always low) data pb7-0 i/o bidirectional databus (output when oe is low) table 42. xa1 and xa0 coding xa1 xa0 action when xtal1 is pulsed 0 0 load flash or eeprom address (high or low address byte determined by bs1) 0 1 load data (high or low data byte for flash determined by bs1) 1 0 load command 1 1 no action, idle atmega161 vcc +5v gnd xtal1 pd1 pd2 pd3 pd4 pd5 pd6 pb7 - pb0 data reset pd7 +12 v bs1 xa0 xa1 oe rdy/bsy pagel pa0 wr
atmega161(l) 103 enter programming mode the following algorithm puts the device in parallel programming mode: 1. apply 4.5 - 5.5v between v cc and gnd. 2. set reset and bs pins to ? 0 ? and wait at least 500 ns. 3. apply 11.5 - 12.5v to reset , and wait for at least 500 ns. chip erase the chip erase will erase the flash and eeprom memories plus lock bits. the lock bits are not reset until the program memory has been completely erased. the fuse bits are not changed. a chip erase must be performed before the flash is reprogrammed. load command ? chip erase ? 1. set xa1, xa0 to ? 10 ? . this enables command loading. 2. set bs1 to ? 0 ? . 3. set data to ? 1000 0000 ? . this is the command for chip erase. 4. give wr a negative pulse. this starts the chip erase. rdy/bsy goes low. 5. wait until rdy/bsy goes high before loading a new command. programming the flash the flash is organized as 128 pages of 128 bytes each. when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be programmed simultaneously. the following procedure describes how to program the entire flash memory: a. load command "write flash" 1. set xa1, xa0 to '10'. this enables command loading. 2. set bs1 to '0'. 3. set data to '0001 0000'. this is the command for write flash. 4. give xtal1 a positive pulse. this loads the command. b. load address low byte 1. set xa1, xa0 to '00'. this enables address loading. 2. set bs1 to '0'. this selects low address. 3. set data = address low byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the address low byte. table 43. command byte bit coding command byte command executed 1000 0000 chip erase 0100 0000 write fuse bits 0010 0000 write lock bits 0001 0000 write flash 0001 0001 write eeprom 0000 1000 read signature bytes 0000 0100 read fuse and lock bits 0000 0010 read flash 0000 0011 read eeprom
atmega161(l) 104 c. load data low byte 1. set bs1 to ? 0 ? . this selects low data byte. 2. set xa1, xa0 to ? 01 ? . this enables data loading. 3. set data = data low byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the data byte. d. latch data low byte give pagel a positive pulse. this latches the data low byte. (see figure 77 for signal waveforms) e. load data high byte 1. set bs1 to ? 1 ? . this selects high data byte. 2. set xa1, xa0 to ? 01 ? . this enables data loading. 3. set data = data high byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the data byte. f. latch data high byte give pagel a positive pulse. this latches the data high byte. g. repeat b through f 64 times to fill the page buffer. to address a page in the flash, 7 bits are needed (128 pages). the 5 most significant bits are read from address high byte as described in section ? h ? below. the two least significant page address bits however, are the two most significant bits (bit7 and bit6) of the latest loaded address low byte as described in section ? b ? . h. load address high byte 1. 1. set xa1, xa0 to '00'. this enables address loading. 2. set bs1 to '1'. this selects high address. 3. set data = address high byte ($00 - $1f). 4. give xtal1 a positive pulse. this loads the address high byte. i. program page 1. give wr a negative pulse. this starts programming of the entire page of data. rdy/bsy goes low. 2. wait until rdy/bsy goes high. (see figure 78 for signal waveforms) j. end page programming 1. 1. set xa1, xa0 to '10'. this enables command loading. 2. set data to '0000 0000'. this is the command for no operation. 3. give xtal1 a positive pulse. this loads the command, and the internal write signals are reset. k. repeat a through j 128 times or until all data have been programmed.
atmega161(l) 105 figure 77. programming the flash waveforms figure 78. programming the flash waveforms (continued) $10 addr. low addr. high data low data xa1 xa2 bs1 xtal1 rdy/bsy reset wr oe +12v bs2 pagel data high data xa1 xa0 bs1 xtal1 wr rdy/bsy reset +12v oe bs2 pagel
atmega161(l) 106 programming the eeprom the programming algorithm for the eeprom data memory is as follows (refer to ? programming the flash ? on page 103 for details on command, address and data loading): 1. a: load command ? 0001 0001 ? . 2. h: load address high byte ($00 - $01) 3. b: load address low byte ($00 - $ff) 4. e: load data low byte ($00 - $ff) l: write data low byte 1. set bs to ? 0 ? . this selects low data. 2. give wr a negative pulse. this starts programming of the data byte. rdy/bsy goes low. 3. wait until to rdy/bsy goes high before programming the next byte. (see figure 79 for signal waveforms) the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered.  the command needs only be loaded once when writing or reading multiple memory locations.  address high byte needs only be loaded before programming a new 256 word page in the eeprom.  skip writing the data value $ff, that is the contents of the entire eeprom after a chip erase. these considerations also applies to flash, eeprom and signature bytes reading. figure 79. programming the eeprom waveforms $11 addr. high addr. low data low data xa1 xa2 bs1 xtal1 rdy/bsy reset wr oe +12v bs2 pagel
atmega161(l) 107 reading the flash the algorithm for reading the flash memory is as follows (refer to ? programming the flash ? on page 103 for details on command and address loading): 1. a: load command ? 0000 0010 ? . 2. h: load address high byte ($00 - $1f) 3. b: load address low byte ($00 - $ff) 4. set oe to ? 0 ? , and bs1 to ? 0 ? . the flash word low byte can now be read at data. 5. set bs to ? 1 ? . the flash word high byte can now be read at data. 6. set oe to ? 1 ? . reading the eeprom the algorithm for reading the eeprom memory is as follows (refer to ? programming the flash ? on page 103 for details on command and address loading): 1. a: load command ? 0000 0011 ? . 2. h: load address high byte ($00 - $01) 3. b: load address ($00 - $ff) 4. set oe to ? 0 ? , and bs1 to ? 0 ? . the eeprom data byte can now be read at data. 5. set oe to ? 1 ? . programming the fuse bits the algorithm for programming the fuse bits is as follows (refer to ? programming the flash ? on page 103 for details on command and data loading): 1. a: load command ? 0100 0000 ? . 2. c: load data low byte. bit n = ? 0 ? programs and bit n = ? 1 ? erases the fuse bit. bit 6 = bootrst fuse bit bit 5 = spien fuse bit bit 4 = bodlevel fuse bit bit 3 = boden fuse bit bit 2-0 = cksel2..0 fuse bits bit 7 = ? 1 ? . this bit is reserved and should be left unprogrammed ( ? 1 ? ). 3. give wr a negative pulse and wait for rdy/bsy to go high. programming the lock bits the algorithm for programming the lock bits is as follows (refer to ? programming the flash ? on page 103 for details on command and data loading): 1. a: load command ? 0010 0000 ? . 2. d: load data low byte. bit n = ? 0 ? programs the lock bit. bit 5 = boot lock bit12 bit 4 = boot lock bit11 bit 3 = boot lock bit02 bit 2 = boot lock bit01 bit 1 = lock bit2 bit 0 = lock bit1 bit 7-6 = ? 1 ? . these bits are reserved and should be left unprogrammed ( ? 1 ? ). 3. l: write data low byte. the lock bits can only be cleared by executing chip erase.
atmega161(l) 108 reading the fuse and lock bits the algorithm for reading the fuse and lock bits is as follows (refer to ? programming the flash ? on page 103 for details on command loading): 1. a: load command ? 0000 0100 ? . 2. set oe to ? 0 ? , and bs to ? 0 ? . the status of the fuse bits can now be read at data ( ? 0 ? means programmed). bit 6 = bootrst fuse bit bit 5 = spien fuse bit bit 4 = bodlevel fuse bit bit 3 = boden fuse bit bit 2-0 = cksel2..0 fuse bits 3. set oe to ? 0 ? , and bs to ? 1 ? . the status of the lock bits can now be read at data ( ? 0 ? means programmed). bit 5 = boot lock bit12 bit 4 = boot lock bit11 bit 3 = boot lock bit02 bit 2 = boot lock bit01 bit 1 = lock bit2 bit 0 = lock bit1 4. set oe to ? 1 ? . reading the signature bytes the algorithm for reading the signature bytes is as follows (refer to programming the flash for details on command and address loading): 1. a: load command ? 0000 1000 ? . 2. c: load address low byte ($00 - $02). set oe to ? 0 ? , and bs to ? 0 ? . the selected signature byte can now be read at data. 3. set oe to ? 1 ? . parallel programming characteristics figure 80. parallel programming timing data & contol (data, xa0/1, bs1) data write read xtal1 t xhxl t wlwh t dvxh t xlol t oldv t xldx t plwl t wlrh wr rdy/bsy oe pagel t phpl t plbx t bvxh t xlwl t rhbx t ohdz t bvwl wlrl
atmega161(l) 109 notes: 1. t wlrh is valid for the write eeprom, write fuse bits and write lock bits commands. 2. t wlrh_ce is valid for the chip erase command. 3. t wlrh_flash is valid for the write flash command. serial downloading both the flash and eeprom memory arrays can be programmed using the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (output). after reset is set low, the programming enable instruction needs to be executed first before program/erase operations can be executed. when programming the eeprom, an auto-erase cycle is built into the self-timed programming operation (in the serial mode only) and there is no need to first execute the chip erase instruction. the chip erase operation turns the content of every memory location in both the program and eeprom arrays into $ff. the program and eeprom memory arrays have separate address spaces: $0000 to $1fff for program memory and $0000 to $01ff for eeprom memory. either an external system clock is supplied at pin xtal1 or a crystal needs to be connected across pins xtal1 and xtal2.the minimum low and high periods for the serial clock (sck) input are defined as follows: low:> 2 xtal1 clock cycle high:> 2 xtal1 clock cycles table 44. parallel programming characteristics, t a = 25 c 10%, v cc =5v 10% symbol parameter min typ max units v pp programming enable voltage 11.5 12.5 v i pp programming enable current 250 a t dvxh data and control valid before xtal1 high 67 ns t xhxl xtal1 pulse width high 67 ns t xldx data and control hold after xtal1 low 67 ns t xlwl xtal1 low to wr low 67 ns t bvxh bs1 valid before xtal1 high 67 ns t phpl pagel pulse width high 67 ns t plbx bs1 hold after pagel low 67 ns t plwl pagel low to wr low 67 ns t bvwl bs1 valid to wr low 67 ns t rhbx bs1 hold after rdy/bsy high 67 ns t wlwh wr pulse width low 67 ns t wlrl wr low to rdy/bsy low 0 2.5 s t wlrh wr low to rdy/bsy high (1) 0.5 0.7 0.9 ms t wlrh_ce wr low to rdy/bsy high for chip erase (2) 10 14 18 ms t wlrh_flash wr low to rdy/bsy high for write flash (3) 579ms t xlol xtal1 low to oe low 67 ns t oldv oe low to data valid 20 ns t ohdz oe high to data tri-stated 20 ns
atmega161(l) 110 serial programming algorithm when writing serial data to the atmega161, data is clocked on the rising edge of sck. when reading data from the atmega161, data is clocked on the falling edge of sck. see figure 81, figure 82 and table 47 for timing details. to program and verify the atmega161 in the serial programming mode, the following sequence is recommended (see four byte instruction formats in table 46 ): 1. power-up sequence: apply power between v cc and gnd while reset and sck are set to ? 0 ? . if a crystal is not connected across pins xtal1 and xtal2, apply a clock signal to the xtal1 pin. in some systems, the programmer can not guarantee that sck is held low during power-up. in this case, reset must be given a positive pulse of at least two xtal1 cycles duration after sck has been set to ? 0 ? . 2. wait for at least 20 ms and enable serial programming by sending the programming enable serial instruction to pin mosi/pb5. 3. the serial programming instructions will not work if the communication is out of synchronization. when in sync. the second byte ($53), will echo back when issuing the third byte of the programming enable instruction. whether the echo is correct or not, all 4 bytes of the instruction must be transmitted. if the $53 did not echo back, give reset a positive pulse and issue a new programming enable command. 4. if a chip erase is performed (must be done to erase the flash), give reset a positive pulse, and start over from step 2. 5. the flash is programmed one page at a time. the memory page is loaded one byte at a time by supplying the 6 lsb of the address and data together with the load program memory page instruction. the program memory page is stored by loading the write program memory page instruction with the 7 msb of the address. if polling is not used, the user must wait at least t wd_flash before issuing the next page. (please refer to table 45). accessing the serial programming interface before the flash write operation completes can result in incorrect programming. 6. the eeprom array is programmed one byte at a time by supplying the address and data together with the appro- priate write instruction. an eeprom memory location is first automatically erased before new data is written. if polling is not used, the user must wait at least t wd_eeprom before issuing the next byte. (please refer to table 45). in a chip erased device, no $ffs in the data file(s) need to be programmed. 7. any memory location can be verified by using the read instruction which returns the content at the selected address at serial output miso/pb6. 8. at the end of the programming session, reset can be set high to commence normal operation. 9. power-off sequence (if needed): set xtal1 to ? 0 ? (if a crystal is not used). set reset to ? 1 ? . turn v cc power-off data polling flash when a page is being programmed into the flash, reading an address location within the page being programmed will give the value $ff. at the time the device is ready for a new page, the programmed value will read correctly. this is used to determine when the next page can be written. note that the entire page is written simultaneously and any address within the page can be used for polling. data polling of the flash will not work for the value $ff, so when programming this value, the user will have to wait for at least t wd_flash before programming the next page. as a chip-erased device contains $ff in all locations, programming of addresses that are meant to contain $ff, can be skipped. see table 45 for t wd_flash value.
atmega161(l) 111 data polling eeprom when a new byte has been written and is being programmed into eeprom, reading the address location being pro- grammed will give the value $ff. at the time the device is ready for a new byte, the programmed value will read correctly. this is used to determine when the next byte can be written. this will not work for the value $ff, but the user should have the following in mind: as a chip-erased device contains $ff in all locations, programming of addresses that are meant to contain $ff, can be skipped. this does not apply if the eeprom is re-programmed without chip-erasing the device. in this case, data polling cannot be used for the value $ff, and the user will have to wait at least t wd_eeprom before programming the next byte. see table 45 for t wd_eeprom value. figure 81. serial programming waveforms table 45. minimum wait delay before writing the next flash or eeprom location symbol 3.2v 3.6v 4.0v 5.0v t wd_flash 28 ms 22 ms 18 ms 11 ms t wd_eeprom 9 ms 7 ms 6 ms 4 ms msb msb lsb lsb serial clock input pb7(sck) serial data input pb5 (mosi) pb6 (miso) sample serial data output
atmega161(l) 112 . note: a = address high bits b = address low bits h = 0 - low byte, 1 - high byte o = data out i = data in x = don ? t care 1 = lock bit 1 2 = lock bit 2 3 = boot lock bit01 4 = boot lock bit02 5 = boot lock bit11 6 = boot lock bit12 7 = cksel0 fuse 8 = cksel1 fuse 9 = cksel2 fuse a = boden fuse b = bodlevel fuse c = spien fuse d = bootrst fuse table 46. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 xxxa aaaa bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a : b . load program memory page 0100 h 000 xxxx xxxx xx bb bbbb iiii iiii write h (high or low) data i to program memory page at word address b . write program memory page 0100 1100 xxxa aaaa bb xx xxxx iiii iiii write program memory page at address a : b . read eeprom memory 1010 0000 xxxx xxx a bbbb bbbb oooo oooo read data o from eeprom memory at address a : b . write eeprom memory 1100 0000 xxxx xxx a bbbb bbbb iiii iiii write data i to eeprom memory at address a : b . read lock bits 0101 1000 xxxx xxxx xxxx xxxx xx 65 4321 read lock bits. ? 0 ? = programmed, ? 1 ? = unprogrammed. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 65 4321 write lock bits. set bits 6 - 1 = ? 0 ? to program lock bits. read signature byte 0011 0000 xxxx xxxx xxxx xx bb oooo oooo read signature byte o at address b . write fuse bits 1010 1100 101x xxxx xxxx xxxx 1 dcb a987 set bits d - a , 9 - 7 = ? 0 ? to program, ? 1 ? to unprogram read fuse bits 1010 0000 xxxx xxxx xxxx xxxx x dcb a987 read fuse bits. ? 0 ? = programmed, ? 1 ? = unprogrammed
atmega161(l) 113 serial programming characteristics figure 82. serial programming timing electrical characteristics absolute maximum ratings* table 47. serial programming characteristics, t a = -40 c to 85 c, v cc = 2.7 - 5.5v (unless otherwise noted) symbol parameter min typ max units 1/t clcl oscillator frequency (v cc = 2.7 - 5.5v )0 4mhz t clcl oscillator period (v cc = 2.7 - 5.5v )250 ns 1/t clcl oscillator frequency (v cc = 4.0 - 5.5v )0 8mhz t clcl oscillator period (v cc = 4.0 - 5.5v )125 ns t shsl sck pulse width high 2 t clcl ns t slsh sck pulse width low 2 t clcl ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns t sliv sck low to miso valid 10 16 32 ns operating temperature.................................. -55 c to +125 c *notice: stresses beyond those listed under ? absolute maximum ratings ? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ..................................... -65 c to +150 c voltage on any pin except reset with respect to ground ................................-1.0v to v cc +0.5v voltage on reset with respect to ground......-1.0v to +13.0v maximum operating voltage ............................................ 6.6v dc current per i/o pin ............................................... 40.0 ma dc current v cc and gnd pins................................ 200.0 ma mosi miso sck t ovsh t shsl t slsh t shox t sliv
atmega161(l) 114 notes: 1. ? max ? means the highest value where the pin is guaranteed to be read as low 2. ? min ? means the lowest value where the pin is guaranteed to be read as high 3. although each i/o port can sink more than the test conditions (20 ma at vcc = 5v, 10 ma at vcc = 3v) under steady state conditions (non-transient), the following must be observed: 1] the sum of all iol, for all ports, should not exceed 200 ma. 2] the sum of all iol, for ports b0 - b7, d0 - d7 and xtal2, should not exceed 100 ma. 3] the sum of all iol, for ports a0 - a7, ale, oc1b and c0 - c7 should not exceed 100 ma. if iol exceeds the test condition, vol may exceed the related specification. pins are not guaranteed to sink current greater than the listed test condition. 4. although each i/o port can source more than the test conditions (3 ma at vcc = 5v, 1.5 ma at vcc = 3v) under steady state conditions (non-transient), the following must be observed: 1] the sum of all ioh, for all ports, should not exceed 200 ma. 2] the sum of all ioh, for ports b0 - b7, d0 - d7 and xtal2, should not exceed 100 ma. 3] the sum of all ioh, for ports a0 - a7, ale, oc1b and c0 - c7 should not exceed 100 ma. if ioh exceeds the test condition, voh may exceed the related specification. pins are not guaranteed to source current greater than the listed test condition. 5. minimum v cc for power-down is 2v. dc characteristics t a = -40 c to 85 c, v cc = 2.7v to 5.5v (unless otherwise noted) symbol parameter condition min typ max units v il input low voltage (except xtal1) -0.5 0.3 v cc (1) v v il1 input low voltage (xtal1) -0.5 0.2 v cc (1) v v ih input high voltage (except xtal1, reset ) 0.6 v cc (2) v cc + 0.5 v v ih1 input high voltage (xtal1) 0.8 v cc (2) v cc + 0.5 v v ih2 input high voltage (reset ) 0.9 v cc (2) v cc + 0.5 v v ol output low voltage (3) (ports a,b,c,d) i ol = 20 ma, v cc = 5v i ol = 10 ma, v cc = 3v 0.6 0.5 v v v oh output high voltage (4) (ports a,b,c,d) i oh = -3 ma, v cc = 5v i oh = -1.5 ma, v cc = 3v 4.2 2.3 v v i il input leakage current i/o pin vcc = 5.5v , pin low (absolute value) 8.0 a i ih input leakage current i/o pin vcc = 5.5 v, pin high (absolute value) 980 na rrst reset pull-up resistor 100 500 k ? r i/o i/o pin pull-up resistor 35 120 k ? i cc power supply current active mode, v cc = 3v, 4mhz 3.0 ma idle mode v cc = 3v, 4mhz 1.2 ma power-down mode (5) wdt enabled, v cc = 3v 9 15.0 a wdt disabled, v cc = 3v <1 2.0 a v acio analog comparator input offset voltage v cc = 5v 40 mv i aclk analog comparator input leakage current v cc = 5v v in = v cc /2 -50 50 na t acpd analog comparator propagation delay v cc = 2.7v v cc = 4.0v 750 500 ns
atmega161(l) 115 external clock drive waveforms figure 83. external clock note: see ? external data memory timing ? on page 116. for a description of how the duty cycle influences the timing for the external data memory table 48. external clock drive symbol parameter v cc = 2.7v to 5.5 vv cc = 4.0v to 5.5 v units min max min max 1/t clcl oscillator frequency 0 4 0 8 mhz t clcl clock period 250 125 ns t chcx high time 100 50 ns t clcx low time 100 50 ns t clch rise time 1.6 0.5 ? t chcl fall time 1.6 0.5 ? vil1 vih1
atmega161(l) 116 external data memory timing notes: 1. this assumes 50% clock duty cycle. the half period is actually the high time of the external clock, xtal1. 2. this assumes 50% clock duty cycle. the half period is actually the low time of the external clock, xtal1. table 49. external data memory characteristics, 4.0 - 5.5v, no wait state symbol parameter 8 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 1t lhll ale pulse width tbd t clcl - tbd ns 2t avll address valid a to ale low tbd 0.5t clcl - tbd (1) ns 3a t llax_st address hold after ale low, st/std/sts instructions tbd tbd ns 3b t llax_ld address hold after ale low, ld/ldd/lds instructions tbd tbd ns 4t avllc address valid c to ale low tbd 0.5t clcl - tbd (1) ns 5t avrl address valid to rd low tbd 1.0t clcl - tbd ns 6t avwl address valid to wr low tbd 1.0t clcl - tbd ns 7t llwl ale low to wr low tbd tbd 0.5t clcl - tbd (2) 0.5t clcl - tbd (2) ns 8t llrl ale low to rd low tbd tbd 0.5t clcl - tbd (2) 0.5t clcl - tbd (2) ns 9t dvrh data setup to rd high tbd tbd ns 10 t rldv read low to data valid tbd tbd ns 11 t rhdx data hold after rd high tbd tbd ns 12 t rlrh rd pulse width tbd 1.0t clcl - tbd ns 13 t dvwl data setup to wr low tbd 0.5t clcl - tbd (1) ns 14 t whdx data hold after wr high tbd 0.5t clcl - tbd (1) ns 15 t dvwh data valid to wr high tbd 1.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 1.0t clcl - tbd ns table 50. external data memory characteristics, 4.0 - 5.5v, 1 cycle wait state symbol parameter 8 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 10 t rldv read low to data valid tbd 2.0t clcl - tbd ns 12 t rlrh rd pulse width tbd 2.0t clcl - tbd ns 15 t dvwh data valid to wr high tbd 2.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 2.0t clcl - tbd ns
atmega161(l) 117 table 51. external data memory characteristics, 4.0 - 5.5v, srwn1 = 1, srwn0 = 0 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 10 t rldv read low to data valid tbd 3.0t clcl - tbd ns 12 t rlrh rd pulse width tbd 3.0t clcl - tbd ns 15 t dvwh data valid to wr high tbd 3.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 3.0t clcl - tbd ns table 52. external data memory characteristics, 4.0 - 5.5v, srwn1 = 1, srwn0 = 1 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 10 t rldv read low to data valid tbd 3.0t clcl - tbd ns 12 t rlrh rd pulse width tbd 3.0t clcl - tbd ns 14 t whdx data hold after wr high tbd 1.5t clcl - tbd ns 15 t dvwh data valid to wr high tbd 3.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 3.0t clcl - tbd ns table 53. external data memory characteristics, 2.7 - 5.5v, no wait state symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 1t lhll ale pulse width tbd t clcl - tbd ns 2t avll address valid a to ale low tbd 0.5t clcl - tbd ns 3a t llax_st address hold after ale low, st/std/sts instructions tbd tbd ns 3b t llax_ld address hold after ale low, ld/ldd/lds instructions tbd tbd ns 4t avllc address valid c to ale low tbd 0.5t clcl - tbd ns 5t avrl address valid to rd low tbd 1.0t clcl - tbd ns 6t avwl address valid to wr low tbd 1.0t clcl - tbd ns 7t llwl ale low to wr low tbd tbd 0.5t clcl - tbd 0.5t clcl - tbd ns 8t llrl ale low to rd low tbd tbd 0.5t clcl - tbd 0.5t clcl - tbd ns 9t dvrh data setup to rd high tbd tbd ns 10 t rldv read low to data valid tbd tbd ns 11 t rhdx data hold after rd high tbd tbd ns
atmega161(l) 118 notes: 1. this assumes 50% clock duty cycle. the half period is actually the high time of the external clock, xtal1. 2. this assumes 50% clock duty cycle. the half period is actually the low time of the external clock, xtal1. 12 t rlrh rd pulse width tbd 1.0t clcl - tbd ns 13 t dvwl data setup to wr low tbd 0.5t clcl - tbd ns 14 t whdx data hold after wr high tbd 0.5t clcl - tbd ns 15 t dvwh data valid to wr high tbd 1.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 1.0t clcl - tbd ns table 54. external data memory characteristics, 2.7 - 5.5v, srwn1 = 0, srwn0 = 1 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 10 t rldv read low to data valid tbd 2.0t clcl - tbd ns 12 t rlrh rd pulse width tbd 2.0t clcl - tbd ns 15 t dvwh data valid to wr high tbd 2.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 2.0t clcl - tbd ns table 55. external data memory characteristics, 2.7 - 5.5v, srwn1 = 1, srwn0 = 0 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 10 t rldv read low to data valid tbd 3.0t clcl - tbd ns 12 t rlrh rd pulse width tbd 3.0t clcl - tbd ns 15 t dvwh data valid to wr high tbd 3.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 3.0t clcl - tbd ns table 56. external data memory characteristics, 2.7 - 5.5v, srwn1 = 1, srwn0 = 1 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 10 t rldv read low to data valid tbd 3.0t clcl - tbd ns 12 t rlrh rd pulse width tbd 3.0t clcl - tbd ns 14 t whdx data hold after wr high tbd 1.5t clcl - tbd ns 15 t dvwh data valid to wr high tbd 3.0t clcl - tbd ns 16 t wlwh wr pulse width tbd 3.0t clcl - tbd ns table 53. external data memory characteristics, 2.7 - 5.5v, no wait state (continued) symbol parameter 4 mhz oscillator variable oscillator unit min max min max
atmega161(l) 119 figure 84. external memory timing (srwn1 = 0, srwn0 = 0 figure 85. external memory timing (srwn1 = 0, srwn0 = 1) ale wr rd address [15..8] address xx xx read data / address [7..0] address data xx write data / address [7..0] address data xx system clock ? t1 t2 t3 t4 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data ale wr rd address [15..8] address xx xx read data / address [7..0] address data xx write data / address [7..0] address data xx t1 t2 t3 t5 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data system clock ? t4
atmega161(l) 120 figure 86. external memory timing (srwn1 = 1, srwn0 = 0) figure 87. external memory timing (srwn1 = 1, srwn0 = 1) note: the ale pulse in the last period (t4 - t7) is only present if the next instruction accesses the ram (internal or external) . the data and address will only change in t4 - t7 if ale is present (the next instruction accesses the ram). ale wr rd address [15..8] address xx xx read data / address [7..0] address data xx write data / address [7..0] address data xx t1 t2 t3 t6 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data system clock ? t4 t5 ale wr rd address [15..8] address xx xx read data / address [7..0] address data xx write data / address [7..0] address data xx t1 t2 t3 t7 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data t4 t5 t6 system clock ?
atmega161(l) 121 typical characteristics ? preliminary data analog comparator offset voltage is measured as absolute offset figure 88. analog comparator offset voltage vs, common mode voltage figure 89. analog comparator offset voltage vs. common mode voltage 0 2 4 6 8 10 12 14 16 18 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 analog comparator offset voltage vs. v = 5v cc common mode voltage common mode voltage (v) offset voltage (mv) t = 85 ? c a t = 25 ? c a 0 2 4 6 8 10 0 0.5 1 1.5 2 2.5 3 analog comparator offset voltage vs. common mode voltage common mode voltage (v) offset voltage (mv) v = 2.7v cc t = 85 ? c a t = 25 ? c a
atmega161(l) 122 figure 90. analog comparator input leakage current figure 91. watchdog oscillator frequency vs. v cc 60 50 40 30 20 10 0 -10 0 0.5 1.5 1 2 2.5 3.5 3 4 4.5 5 6 6.5 7 5.5 analog comparator input leakage current t = 25 ? c a i (na) aclk v (v) in v = 6v cc 0 200 400 600 800 1000 1200 1400 1600 2 2.5 3 3.5 4 4.5 5 5.5 6 t = 85 ? c a t = 25 ? c a watchdog oscillator frequency vs. v cc v (v) cc f (khz) rc
atmega161(l) 123 sink and source capabilities of i/o ports are measured on one pin at a time. figure 92. pull-up resistor current vs. input voltage figure 93. pull-up resistor current vs. input voltage 0 20 40 60 80 100 120 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 pull-up resistor current vs. input voltage v = 5v cc i ( a) op v (v) op t = 85 ? c a t = 25 ? c a 0 5 10 15 20 25 30 0 0.5 1 1.5 2 2.5 3 pull-up resistor current vs. input voltage i ( a) op v (v) op v = 2.7v cc t = 85 ? c a t = 25 ? c a
atmega161(l) 124 figure 94. i/o pin sink current vs. output voltage figure 95. i/o pin source current vs. output voltage 0 10 20 30 40 50 60 70 0 0.5 1 1.5 2 2.5 3 v = 5v cc i (ma) ol v (v) ol t = 85 ? c a t = 25 ? c a i/o pin sink current vs. output voltage 0 2 4 6 8 10 12 14 16 18 20 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i/o pin source current vs. output voltage v = 5v cc i (ma) oh v (v) oh t = 85 ? c a t = 25 ? c a
atmega161(l) 125 figure 96. i/o pin sink current vs. output voltage figure 97. i/o pin source current vs. output voltage 0 5 10 15 20 25 0 0.5 1 1.5 2 i (ma) ol v (v) ol t = 85 ? c a t = 25 ? c a i/o pin sink current vs. output voltage v = 2.7v cc 0 1 2 3 4 5 6 0 0.5 1 1.5 2 2.5 3 i/o pin source current vs. output voltage i (ma) oh v (v) oh t = 85 ? c a t = 25 ? c a v = 2.7v cc
atmega161(l) 126 figure 98. i/o pin input threshols vs. v cc figure 99. i/o pin input hysteresis vs. v cc 0 0.5 1 1.5 2 2.5 2.7 4.0 5.0 threshold voltage (v) v cc i/o pin input threshold voltage vs. v cc t = 25 ? c a 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 2.7 4.0 5.0 input hysteresis (v) v cc i/o pin input hysteresis vs. v cc t = 25 ? c a
atmega161(l) 127 register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page $3f ($5f) sreg i t h s v n z c 21 $3e ($5e) sph sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 22 $3d ($5d) spl sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 22 $3c ($5c) reserved $3b ($5b) gimsk int1 int0 int2 - - - - -29 $3a ($5a) gifr intf1 intf0 intf2 30 $39 ($59) timsk toie1 ocie1a ocie1b ocie2 ticie1 toie2 toie0 ocie0 30 $38 ($58) tifr tov1 ocf1a ocf1b ocfi2 icf1 tov2 tov0 ocif0 31 $37 ($57) spmcr - - - - lbset pgwrt pgers spmen 98 $36 ($56) emcucr sm0 srl2 srl1 srl0 srw01 srw00 srw11 isc2 33 $35 ($55) mcucr sre srw10 se sm1 isc11 isc10 isc01 isc00 32 $34 ($54) mcusr - - - - wdrf borf extrf porf 28 $33 ($53) tccr0 foc0 pwm0 com01 com00 ctc0 cs02 cs01 cs00 38 $32 ($52) tcnt0 timer/counter0 counter register 40 $31 ($51) ocr0 timer/counter0 output compare register 40 $30 ($50) sfior - - - - - - psr2 psr10 36 $2f ($4f) tccr1a com1a1 com1a0 com1b1 com1b0 foc1a foc1b pwm11 pwm10 46 $2e ($4e) tccr1b icnc1 ices1 - - ctc1 cs12 cs11 cs10 47 $2d ($4d) tcnt1h timer/counter1 - counter register high byte 48 $2c ($4c) tcnt1l timer/counter1 - counter register low byte 48 $2b ($4b) ocr1ah timer/counter1 - output compare register a high byte 49 $2a ($4a) ocr1al timer/counter1 - output compare register a low byte 49 $29 ($49) ocr1bh timer/counter1 - output compare register b high byte 49 $28 ($48) ocr1bl timer/counter1 - output compare register b low byte 49 $27 ($47) tccr2 foc2 pwm2 com21 com20 ctc2 cs22 cs21 cs20 38 $26 ($46) assr - - - - as20 tcon2ub ocr2ub tcr2ub 43 $25 ($45) icr1h timer/counter1 - input capture register high byte 49 $24 ($44) icr1l timer/counter1 - input capture register low byte 49 $23 ($43) tcnt2 timer/counter2 counter register 40 $22 ($42) ocr2 timer/counter2 output compare register 40 $21 ($41) wdtcr - - - wdtoe wde wdp2 wdp1 wdp0 53 $20 ($40) ubrrhi ubrr1[11:8] ubrr0[11:8] 67 $1f ($3f) eearh - - - - - - - eear8 54 $1e ($3e) eearl eeprom address register low byte 54 $1d ($3d) eedr eeprom data register 54 $1c ($3c) eecr - - - - eerie eemwe eewe eere 54 $1b ($3b) porta porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 77 $1a ($3a) ddra dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 77 $19 ($39) pina pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 77 $18 ($38) portb portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 79 $17 ($37) ddrb ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 79 $16 ($36) pinb pinb7 pinb6 pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 79 $15 ($35) portc portc7 portc6 portc5 portc4 portc3 portc2 portc1 portc0 85 $14 ($34) ddrc ddc7 ddc6 ddc5 ddc4 ddc3 ddc2 ddc1 ddc0 85 $13 ($33) pinc pinc7 pinc6 pinc5 pinc4 pinc3 pinc2 pinc1 pinc0 85 $12 ($32) portd portd7 portd6 portd5 portd4 portd3 portd2 portd1 portd0 87 $11 ($31) ddrd ddd7 ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 87 $10 ($30) pind pind7 pind6 pind5 pind4 pind3 pind2 pind1 pind0 87 $0f ($2f) spdr spi data register 60 $0e ($2e) spsr spif wcol - - - - - spi2x 59 $0d ($2d) spcr spie spe dord mstr cpol cpha spr1 spr0 59 $0c ($2c) udr0 uart0 i/o data register 64 $0b ($2b) ucsr0a rxc0 txc0 udre0 fe0 or0 - u2x0 mpcm0 64 $0a ($2a) ucsr0b rxcie0 txcie0 udrie0 rxen0 txen0 chr90 rxb80 txb80 65 $09 ($29) ubrr0 uart0 baud rate register 68 $08 ($28) acsr acd ainbg aco aci acie acic acis1 acis0 70 $07 ($27) porte - - - - - porte2 porte1 porte0 92
atmega161(l) 128 notes: 1. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory address es should never be written. 2. some of the status flags are cleared by writing a logical one to them. note that the cbi and sbi instructions will operate on all bits in the i/o register, writing a one back into any flag read as set, thus clearing the flag. the cbi and sbi instruction s work with registers $00 to $1f only. $06 ($26) ddre - - - - - dde2 dde1 dde0 92 $05 ($25) pine - - - - - pine2 pine1 pine0 92 $04 ($24) reserved $03 ($23) udr1 uart1 i/o data register 64 $02 ($22) ucsr1a rxc1 txc1 udre1 fe1 or1 - u2x1 mpcm1 64 $01 ($21) ucsr1b rxcie1 txcie1 udrie1 rxen1 txen1 chr91 rxb81 txb81 65 $00 ($20) ubrr1 uart1 baud rate register 68 register summary (continued) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page
atmega161(l) 129 instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl,k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl,k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? k z,n,v 1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one s complement rd $ff ? rd z,c,n,v 1 neg rd two s complement rd $00 ? rd z,c,n,v,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v 1 cbr rd,k clear bit(s) in register rd rd ? ($ff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd ? 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd $ff none 1 mul rd, rr multiply unsigned r1:r0 rd x rr z,c 2 muls rd, rr multiply signed r1:r0 rd x rr z,c 2 mulsu rd, rr multiply signed with unsigned r1:r0 rd x rr z,c 2 fmul rd, rr fractional multiply unsigned r1:r0 (rd x rr) << 1 z,c 2 fmuls rd, rr fractional multiply signed r1:r0 (rd x rr) << 1 z,c 2 fmulsu rd, rr fractional multiply signed with unsigned r1:r0 (rd x rr) << 1 z,c 2 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc z none 2 jmp k direct jump pc knone3 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc znone3 call k direct subroutine call pc knone4 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, n,v,c,h 1 cpc rd,rr compare with carry rd ? rr ? c z, n,v,c,h 1 cpi rd,k compare register with immediate rd ? k z, n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic p, b skip if bit in i/o register cleared if (p(b)=0) pc pc + 2 or 3 none 1/2/3 sbis p, b skip if bit in i/o register is set if (p(b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2
atmega161(l) 130 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd,y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z+1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st - x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st - y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q,rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q,rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc rd (z), z z+1 none 3 spm store program memory (z) r1:r0 none - in rd, p in port rd pnone1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 bit and bit-test instructions sbi p,b set bit in i/o register i/o(p,b) 1none2 cbi p,b clear bit in i/o register i/o(p,b) 0none2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v 1 ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 instruction set summary (continued) mnemonics operands description operation flags #clocks
atmega161(l) 131 cls clear signed test flag s 0 s 1 sev set twos complement overflow. v 1v1 clv clear twos complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 clh clear half carry flag in sreg h 0 h 1 nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 3 wdr watchdog reset (see specific descr. for wdr/timer) none 1 instruction set summary (continued) mnemonics operands description operation flags #clocks
atmega161(l) 132 ordering information speed (mhz) power supply ordering code package operation range 4 2.7 - 5.5v atmega161-4ac ATMEGA161-4JC atmega161-4pc 44a 44j 40p6 commercial (0 c to 70 c) atmega161-4ai atmega161-4ji atmega161-4pi 44a 44j 40p6 industrial (-40 c to 85 c) 8 4.0 - 5.5v atmega161-8ac atmega161-8jc atmega161-8pc 44a 44j 40p6 commercial (0 c to 70 c) atmega161-8ai atmega161-8ji atmega161-8pi 44a 44j 40p6 industrial (-40 c to 85 c) package type 44a 44-lead, thin (1.0 mm) plastic gull-wing quad flat package (tqfp) 44j 44-lead, plastic j-leaded chip carrier (plcc) 40p6 40-lead, 0.600 wide, plastic dual-in-line package (pdip)
atmega161(l) 133 packaging information *controlling dimension: millimeters 1.20(0.047) max 10.10(0.394) 9.90(0.386) sq 12.21(0.478) 11.75(0.458) sq 0.75(0.030) 0.45(0.018) 0.15(0.006) 0.05(0.002) 0.20(.008) 0.09(.003) 0 7 0.80(0.031) bsc pin 1 id 0.45(0.018) 0.30(0.012) .045(1.14) x 45 pin no. 1 identify .045(1.14) x 30 - 45 .012(.305) .008(.203) .021(.533) .013(.330) .630(16.0) .590(15.0) .043(1.09) .020(.508) .120(3.05) .090(2.29) .180(4.57) .165(4.19) .500(12.7) ref sq .032(.813) .026(.660) .050(1.27) typ .022(.559) x 45 max (3x) .656(16.7) .650(16.5) .695(17.7) .685(17.4) sq sq 2.07(52.6) 2.04(51.8) pin 1 .566(14.4) .530(13.5) .090(2.29) max .005(.127) min .065(1.65) .015(.381) .022(.559) .014(.356) .065(1.65) .041(1.04) 0 15 ref .690(17.5) .610(15.5) .630(16.0) .590(15.0) .012(.305) .008(.203) .110(2.79) .090(2.29) .161(4.09) .125(3.18) seating plane .220(5.59) max 1.900(48.26) ref 44a, 44-lead, thin (1.0 mm) plastic gull-wing quad flat package (tqfp) dimensions in millimeters and (inches)* 44j, 44-lead, plastic j-leaded chip carrier (plcc) dimensions in inches and (millimeters) 40p6, 40-lead, 0.600" wide, plastic dual-in-line package (pdip) dimensions in inches and (millimeters) jedec standard ms-011 ac
? atmel corporation 1999. atmel corporation makes no warranty for the use of its products, other than those expressly contained in the company s standard war- ranty which is detailed in atmel s terms and conditions located on the company s web site. the company assumes no responsibility for any errors which may appear in this document, reserves the right to change devices or specifications detailed herein at any tim e without notice, and does not make any commitment to update the information contained herein. no licenses to patents or other intellectu al prop- erty of atmel are granted by the company in connection with the sale of atmel products, expressly or by implication. atmel s products are not authorized for use as critical components in life support devices or systems. atmel headquarters atmel operations corporate headquarters 2325 orchard parkway san jose, ca 95131 tel (408) 441-0311 fax (408) 487-2600 europe atmel u.k., ltd. coliseum business centre riverside way camberley, surrey gu15 3yl england tel (44) 1276-686-677 fax (44) 1276-686-697 asia atmel asia, ltd. room 1219 chinachem golden plaza 77 mody road tsimhatsui east kowloon hong kong tel (852) 2721-9778 fax (852) 2722-1369 japan atmel japan k.k. 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel (81) 3-3523-3551 fax (81) 3-3523-7581 atmel colorado springs 1150 e. cheyenne mtn. blvd. colorado springs, co 80906 tel (719) 576-3300 fax (719) 540-1759 atmel rousset zone industrielle 13106 rousset cedex france tel (33) 4-4253-6000 fax (33) 4-4253-6001 fax-on-demand north america: 1-(800) 292-8635 international: 1-(408) 441-0732 e-mail literature@atmel.com web site http://www.atmel.com bbs 1-(408) 436-4309 printed on recycled paper. 1228a 08/99/xm marks bearing and/or are registered trademarks and trademarks of atmel corporation. terms and product names in this document may be trademarks of others.


▲Up To Search▲   

 
Price & Availability of ATMEGA161-4JC

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X